maven生命周期以及插件goal介绍

maven生命周期分为三套,分别是clean、default和site,每个生命周期相互独立,互不影响。每个生命周期包含一些阶段(phase)

  • clean生命周期主要是用来清理项目
  • default生命周期主要是构建项目
  • site生命周期主要是建立项目站点

clean生命周期包含phase如下:

  1. pre-clean执行一些清理前需要完成的工作。
  2. clean清理上一次构建生成的文件。
  3. post-clean执行一些清理后需要完成的工作。

default生命周期包含phase如下:

  1. validate
  2. initialize
  3. generate-sources
  4. process-sources 处理资源文件,对变量进行替换工作
  5. generate-resources
  6. process-resources 复制resource文件到输出目录
  7. compile 编译java文件并复制到输出目录
  8. process-classes
  9. generate-test-sources
  10. process-test-sources 处理测试资源文件,对变量进行替换工作
  11. generate-test-resources
  12. process-test-resources 复制测试resource文件到输出目录
  13. test-compile 编译测试java文件并复制到输出目录
  14. process-test-classes
  15. test 执行测试用例
  16. prepare-package
  17. package 项目打包,比如Jar包等
  18. pre-integration-test
  19. integration-test
  20. post-integration-test
  21. verify 打包之后,install到本地仓库之前阶段,比如maven-source-plugin的goal:jar-no-fork就是绑定在这个阶段
  22. install 将包安装到本地仓库
  23. deploy 将包复制到远程仓库。

site生命周期包含phase如下:

  1. pre-site
  2. site
  3. post-site
  4. site-deploy

maven的核心仅仅定义了生命周期,具体的任务是交由插件来完成的,比如执行mvn clean的时候,实际上就是下载插件maven-clean-plugin,如果项目里声明了插件版本,就用项目里的插件版本,否则用超级pom里的插件版本。

每个插件执行任务都是要跟生命周期绑定的,mvn clean实际上是绑定了clean生命周期的clean阶段,跟阶段绑定都是由插件的目标(goal)来完成的,mvn clean 默认的目标是clean,详细写法:mvn clean:clean,具体哪些插件有哪些目标可以参考maven官网有详细的指南。

 

 

 

还有哪些默认绑定

 

下面执行一段mvn clean install,可以很清楚的对应。

D:\zhfgit\jcdemo>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:jcdemo >-------------------------
[INFO] Building jcdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jcdemo ---
[INFO] Deleting D:\zhfgit\jcdemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jcdemo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to D:\zhfgit\jcdemo\target\classes
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/InheritThreadLocalTest.java:[11,24] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,31] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[16,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 某些输入文件使用了未经检查或不安全的操作。
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jcdemo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\zhfgit\jcdemo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jcdemo ---
[INFO] Surefire report directory: D:\zhfgit\jcdemo\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.jcdemo.DemoTest
this is test_case contextLoads
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.087 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jcdemo ---
[INFO] Building jar: D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jcdemo ---
[INFO] Installing D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\zhfgit\jcdemo\pom.xml to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.220 s
[INFO] Finished at: 2022-01-13T14:15:15+08:00
[INFO] ------------------------------------------------------------------------

 

编译test类以及resource文件,但是不执行test case,mvn clean install -DskipTest=true

D:\zhfgit\jcdemo>mvn clean install -DskipTests=true
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:jcdemo >-------------------------
[INFO] Building jcdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jcdemo ---
[INFO] Deleting D:\zhfgit\jcdemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jcdemo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to D:\zhfgit\jcdemo\target\classes
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/InheritThreadLocalTest.java:[11,24] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,31] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[16,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 某些输入文件使用了未经检查或不安全的操作。
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jcdemo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\zhfgit\jcdemo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jcdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jcdemo ---
[INFO] Building jar: D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jcdemo ---
[INFO] Installing D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\zhfgit\jcdemo\pom.xml to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.508 s
[INFO] Finished at: 2022-01-13T14:20:27+08:00
[INFO] ------------------------------------------------------------------------

不编译test类又不执行测试用例,mvn clean install -Dmaven.test.skip=true

D:\zhfgit\jcdemo>mvn clean install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:jcdemo >-------------------------
[INFO] Building jcdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jcdemo ---
[INFO] Deleting D:\zhfgit\jcdemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jcdemo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to D:\zhfgit\jcdemo\target\classes
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/InheritThreadLocalTest.java:[11,24] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[10,31] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/Test2.java:[16,20] 编码GBK的不可映射字符
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 某些输入文件使用了未经检查或不安全的操作。
[WARNING] /D:/zhfgit/jcdemo/src/main/java/com/example/jcdemo/TreadLocalTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jcdemo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jcdemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jcdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jcdemo ---
[INFO] Building jar: D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jcdemo ---
[INFO] Installing D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\zhfgit\jcdemo\pom.xml to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.708 s
[INFO] Finished at: 2022-01-13T14:21:31+08:00
[INFO] ------------------------------------------------------------------------

 

或者在插件的configuration skip标签为true

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<!-- skip为true,不编译test类-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<!-- skip为true,不执行测试用例-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
D:\zhfgit\jcdemo>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:jcdemo >-------------------------
[INFO] Building jcdemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jcdemo ---
[INFO] Deleting D:\zhfgit\jcdemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ jcdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jcdemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ jcdemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.5:test (default-test) @ jcdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jcdemo ---
[INFO] Building jar: D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jcdemo ---
[INFO] Installing D:\zhfgit\jcdemo\target\jcdemo-0.0.1-SNAPSHOT.jar to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\zhfgit\jcdemo\pom.xml to D:\mvnRepository\aliyunRepo\com\example\jcdemo\0.0.1-SNAPSHOT\jcdemo-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.570 s
[INFO] Finished at: 2022-01-13T14:32:20+08:00
[INFO] ------------------------------------------------------------------------

另外除了内置的默认绑定,还可以自定义绑定,比如maven-source-plugin自定义绑定放在executions,可以绑定很多个阶段分别在execution。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
            <execution>
                <id>attach-sources</id>
                <phase>package</phase>
                <goals>
                    <goal>jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
</plugin>

 

posted @ 2022-01-13 00:46  feibazhf  阅读(3139)  评论(0编辑  收藏  举报