Maven 生命周期

1. Maven 構(gòu)建生命周期

Maven 構(gòu)建生命周期就是 Maven 將一個整體任務(wù)劃分為一個個的階段,類似于流程圖,按順序依次執(zhí)行。也可以指定該任務(wù)執(zhí)行到中間的某個階段結(jié)束。
Maven 的內(nèi)部有三個構(gòu)建生命周期,分別是 clean, default, site。其中 default 生命周期的核心階段如下所示:

default lifecycle

2. 如何使用構(gòu)建生命周期來完成構(gòu)建工作

  • 可以指定某個生命周期的階段

執(zhí)行 mvn install 命令,將完成 validate, compile, test, package, verify, install 階段,并將 package 生成的包發(fā)布到本地倉庫中。其中某些帶有連字符的階段不能通過 shell 命令單獨指定。例如:(pre-, post-, or process-*)

mvn install
  • 可以指定多個不同構(gòu)建生命周期的階段

執(zhí)行 mvn clean deploy 命令,首先完成的 clean lifecycle,將以前構(gòu)建的文件清理,然后再執(zhí)行 default lifecycle 的 validate, compile, test, package, verify, insstall, deploy 階段,將 package 階段創(chuàng)建的包發(fā)布到遠(yuǎn)程倉庫中。

mvn clean deploy

3. 階段與插件的關(guān)系

如上所述,Maven 將構(gòu)建過程定義為 default lifecycle,并將 default lifecycle 劃分為一個個的階段 phase,這一系列 phase 僅僅是規(guī)定執(zhí)行順序,至于每個階段做什么工作?由誰來做?答案就在 插件(plugins) 中。
Maven 對工程的所有操作實實在在的都是由 插件 來完成的。一個插件可以支持多種功能,稱之為目標(biāo)(goal),例如:compiler 插件有兩個目標(biāo):compile 和 testCompile,分別實現(xiàn)編譯源代碼 和 編譯測試代碼。
如何將插件與 Maven 的構(gòu)建生命周期綁定在一起呢?通過將插件的目標(biāo)(goal)與 build lifecycle 中 phase 綁定到一起,這樣,當(dāng)要執(zhí)行某個 phase 時,就調(diào)用插件來完成綁定的目標(biāo)。
如下圖所示:從圖中可以看出,每一個階段可以綁定0 個 或 多個目標(biāo),每個插件可以提供 1 個或多個目標(biāo)。


build lifecycle & plugin goal

4. 如何為自己的工程創(chuàng)建構(gòu)建生命周期

  • 設(shè)置不同的 packaging 類型

在 pom.xml 文件中,packaging 類型支持 jar, war, ear, pom 等多種類型,不同的 packaging 類型會使得不同的 phase 綁定不同的 plugin goal。下面是 packaging 類型為 jar 時,phase 與 plugin goal 的映射關(guān)系。

階段 目標(biāo)
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 來進(jìn)行綁定。
例如:將插件 modello-maven-plugin 的 java 目標(biāo)綁定到 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,但是也能正常構(gòu)建工程

你可以能會疑問,默認(rèn)的 pom.xml 文件并沒有配置各種 plugin,但是也能正常構(gòu)建工程?答案是 Maven 自己默認(rèn)指定了 plugin。
下面是一個沒有配置任何 plugin 的 pom.xml,執(zhí)行 mvn install 的輸出日志,從日志中可以看到 一系列的 插件(plugin):版本號:目標(biāo)(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 默認(rèn)插件以及功能
maven 依賴管理
maven-shade-plugin 入門指南
maven-assembly-plugin 入門指南
Introduction to the Build Lifecycle

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 所有項目的構(gòu)建都是有生命周期的,這個生命周期包括:項目清理、初始化、編譯、測試、打包、集成測試、驗證、部署、站點生...
    zlcook閱讀 3,014評論 0 21
  • 1.生命周期 maven有三個獨立的有序的生命周期,分別是clean,default和site。其中clean(用...
    AAA建材王總閱讀 1,279評論 0 0
  • 【補(bǔ)充中】通常一個Java Web項目構(gòu)建過程包括清理、編譯、測試、打包、集成測試、驗證、部署等步驟,Maven從...
    泡泡大腳閱讀 408評論 0 0
  • 殺人者恒殺之,所以他已死! 死得蹊蹺,死得詭異,死得其所! 事實上,這卻是諷刺,縱橫天下的他竟是自殺,天...
    小狗汪汪汪閱讀 452評論 0 0

友情鏈接更多精彩內(nèi)容