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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM