1. Maven 構建生命周期
Maven 構建生命周期就是 Maven 將一個整體任務划分為一個個的階段,類似於流程圖,按順序依次執行。也可以指定該任務執行到中間的某個階段結束。
Maven 的內部有三個構建生命周期,分別是 clean, default, site。其中 default 生命周期的核心階段如下所示:
2. 如何使用構建生命周期來完成構建工作
- 可以指定某個生命周期的階段
執行 mvn install 命令,將完成 validate, compile, test, package, verify, install 階段,並將 package 生成的包發布到本地倉庫中。其中某些帶有連字符的階段不能通過 shell 命令單獨指定。例如:(pre-, post-, or process-*)
mvn install
- 可以指定多個不同構建生命周期的階段
執行 mvn clean deploy 命令,首先完成的 clean lifecycle,將以前構建的文件清理,然后再執行 default lifecycle 的 validate, compile, test, package, verify, insstall, deploy 階段,將 package 階段創建的包發布到遠程倉庫中。
mvn clean deploy
3. 階段與插件的關系
如上所述,Maven 將構建過程定義為 default lifecycle,並將 default lifecycle 划分為一個個的階段 phase,這一系列 phase 僅僅是規定執行順序,至於每個階段做什么工作?由誰來做?答案就在 插件(plugins) 中。
Maven 對工程的所有操作實實在在的都是由 插件 來完成的。一個插件可以支持多種功能,稱之為目標(goal),例如:compiler 插件有兩個目標:compile 和 testCompile,分別實現編譯源代碼 和 編譯測試代碼。
如何將插件與 Maven 的構建生命周期綁定在一起呢?通過將插件的目標(goal)與 build lifecycle 中 phase 綁定到一起,這樣,當要執行某個 phase 時,就調用插件來完成綁定的目標。
如下圖所示:從圖中可以看出,每一個階段可以綁定0 個 或 多個目標,每個插件可以提供 1 個或多個目標。
4. 如何為自己的工程創建構建生命周期
- 設置不同的 packaging 類型
在 pom.xml 文件中,packaging 類型支持 jar, war, ear, pom 等多種類型,不同的 packaging 類型會使得不同的 phase 綁定不同的 plugin goal。下面是 packaging 類型為 jar 時,phase 與 plugin goal 的映射關系。
| 階段 | 目標 |
|---|---|
| process-resources | resources:resources |
| compile | compiler:compile |
| process-test-resources | resources:testResources |
| test-compile | compiler:testCompile |
| test | surefire:test |
| package | jar:jar |
| install | install:install |
| deploy | deploy:deploy |
- 配置 plugin
在 pom.xml 文件中, <build> <plugins> 元素下可以添加 <plugin>,通過指定 goal 和 phase 來進行綁定。
例如:將插件 modello-maven-plugin 的 java 目標綁定到 generate-sources 階段。
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<configuration>
<models>
<model>src/main/mdo/maven.mdo</model>
</models>
<version>4.0.0</version>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
5. 我沒有在 pom.xml 指定任何 plugin,但是也能正常構建工程
你可以能會疑問,默認的 pom.xml 文件並沒有配置各種 plugin,但是也能正常構建工程?答案是 Maven 自己默認指定了 plugin。
下面是一個沒有配置任何 plugin 的 pom.xml,執行 mvn install 的輸出日志,從日志中可以看到 一系列的 插件(plugin):版本號:目標(phase),例如 maven-resources-plugin:2.6:resources (default-resources),maven-compiler-plugin:3.1:compile (default-compile) ,maven-resources-plugin:2.6:testResources (default-testResources),maven-compiler-plugin:3.1:testCompile (default-testCompile),maven-surefire-plugin:2.12.4:test (default-test),maven-jar-plugin:2.4:jar (default-jar) ,maven-install-plugin:2.4:install (default-install),
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building my-app 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/my-app/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app --- [INFO] Surefire report directory: /Users/zhangguanghui/git/my-app/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.mycompany.app.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app --- [INFO] Building jar: /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ my-app --- [INFO] Installing /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar [INFO] Installing /Users/zhangguanghui/git/my-app/pom.xml to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.726 s [INFO] Finished at: 2016-11-20T00:41:11+08:00 [INFO] Final Memory: 15M/310M [INFO] ------------------------------------------------------------------------
5. 完整的 clean, default, site build lifecycle
- clean lifecycle
| phase | function |
|---|---|
| pre-clean execute | execute processes needed prior to the actual project cleaning |
| clean | remove all files generated by the previous build |
| post-clean | execute processes needed to finalize the project cleaning |
- default lifecycle
| phase | function |
|---|---|
| validate | validate the project is correct and all necessary information is available. |
| initialize | initialize build state, e.g. set properties or create directories. |
| generate-sources | generate any source code for inclusion in compilation. |
| process-sources | process the source code, for example to filter any values. |
| generate-resources | generate resources for inclusion in the package. |
| process-resources | copy and process the resources into the destination directory, ready for packaging. |
| compile | compile the source code of the project. |
| process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
| generate-test-sources | generate any test source code for inclusion in compilation. |
| process-test-sources | process the test source code, for example to filter any values. |
| generate-test-resources | create resources for testing. |
| process-test-resources | copy and process the resources into the test destination directory. |
| test-compile | compile the test source code into the test destination directory |
| process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above. |
| test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
| prepare-package | perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above) |
| package | take the compiled code and package it in its distributable format, such as a JAR. |
| pre-integration-test perform | actions required before integration tests are executed. This may involve things such as setting up the required environment. |
| integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
| post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
| verify | run any checks to verify the package is valid and meets quality criteria. |
| install | install the package into the local repository, for use as a dependency in other projects locally. |
| deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
- site lifecycle
| phase | function |
|---|---|
| pre-site | execute processes needed prior to the actual project site generation |
| site | generate the project's site documentation |
| post-site | execute processes needed to finalize the site generation, and to prepare for site deployment |
| site-deploy | deploy the generated site documentation to the specified web server |
6. 參考文檔
參考
maven 入門指南
maven 生命周期
Maven 默認插件以及功能
maven 依賴管理
maven-shade-plugin 入門指南
maven-assembly-plugin 入門指南
Introduction to the Build Lifecycle
