Java-Maven(六):Eclipse中Maven插件的命令操作

时间:2022-01-10 22:03:12

  之前几个章节学习了maven的概念,及maven插件安装后如何创建工程,那么maven工程中是如何使用maven命令呢?本章节将会学习这个话题。

在pom.xml中配置maven命令插件

如果向在maven工程中使用maven命令,就必须在pom.xml文件中配置maven命令插件。接下来基于上一章文章(《Java-Maven(五):Eclipse&Maven下创建java工程&web工程》)中创建的java web maven工程之上,进行pom.xml下配置安装maven命令插件。

Java-Maven(六):Eclipse中Maven插件的命令操作

配置之前pom.xml内容如下:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dx</groupId>
<artifactId>maven-web-project01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-web-project01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-web-project01</finalName>
</build>
</project>

配置之后pom.xml内容如下:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dx</groupId>
<artifactId>maven-web-project01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-web-project01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-web-project01</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 不设置采用最新的插件版本 ,也可以去http://maven.apache.org/components/plugins/maven-compiler-plugin/找自己需要的版本号 -->
<version>3.1</version>
<configuration>
<!-- 源代码使用的开发版本 -->
<source>1.8</source>
<!-- 需要生成的目标class文件的编译版本 -->
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

配置之后,如果在pom.xml上右键-》Run As下包含如下截图内容,就说明安装成功。

Java-Maven(六):Eclipse中Maven插件的命令操作

在maven工程中执行maven命令:

我们可以基于上边的安装后maven命令插件后,测试体验下Maven install的作用,点击后控制台会打印信息:

 [INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-web-project01 Maven Webapp 0.0.-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-web-project01 ---
[WARNING] Using platform encoding (UTF- actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-web-project01 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to D:\workspace4\maven-web-project01\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-web-project01 ---
[WARNING] Using platform encoding (UTF- actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\workspace4\maven-web-project01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-web-project01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.:test (default-test) @ maven-web-project01 ---
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ maven-web-project01 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [maven-web-project01] in [D:\workspace4\maven-web-project01\target\maven-web-project01]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\workspace4\maven-web-project01\src\main\webapp]
[INFO] Webapp assembled in [ msecs]
[INFO] Building war: D:\workspace4\maven-web-project01\target\maven-web-project01.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-web-project01 ---
[INFO] Installing D:\workspace4\maven-web-project01\target\maven-web-project01.war to C:\Users\Administrator\.m2\repository\com\dx\maven-web-project01\0.0.-SNAPSHOT\maven-web-project01-0.0.-SNAPSHOT.war
[INFO] Installing D:\workspace4\maven-web-project01\pom.xml to C:\Users\Administrator\.m2\repository\com\dx\maven-web-project01\0.0.-SNAPSHOT\maven-web-project01-0.0.-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.263 s
[INFO] Finished at: --28T02::+:
[INFO] Final Memory: 18M/201M
[INFO] ------------------------------------------------------------------------