1. Maven 構(gòu)建生命周期
Maven 構(gòu)建生命周期就是 Maven 將一個(gè)整體任務(wù)劃分為一個(gè)個(gè)的階段坟募,類(lèi)似于流程圖寒锚,按順序依次執(zhí)行北滥。也可以指定該任務(wù)執(zhí)行到中間的某個(gè)階段結(jié)束昂勒。
Maven 的內(nèi)部有三個(gè)構(gòu)建生命周期井赌,分別是 clean, default, site谤逼。其中 default 生命周期的核心階段如下所示:
2. 如何使用構(gòu)建生命周期來(lái)完成構(gòu)建工作
- 可以指定某個(gè)生命周期的階段
執(zhí)行 mvn install 命令,將完成 validate, compile, test, package, verify, install 階段仇穗,并將 package 生成的包發(fā)布到本地倉(cāng)庫(kù)中流部。其中某些帶有連字符的階段不能通過(guò) shell 命令單獨(dú)指定。例如:(pre-, post-, or process-*)
mvn install
- 可以指定多個(gè)不同構(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)程倉(cāng)庫(kù)中果漾。
mvn clean deploy
3. 階段與插件的關(guān)系
如上所述,Maven 將構(gòu)建過(guò)程定義為 default lifecycle谷誓,并將 default lifecycle 劃分為一個(gè)個(gè)的階段 phase绒障,這一系列 phase 僅僅是規(guī)定執(zhí)行順序,至于每個(gè)階段做什么工作捍歪?由誰(shuí)來(lái)做户辱?答案就在 插件(plugins) 中。
Maven 對(duì)工程的所有操作實(shí)實(shí)在在的都是由 插件 來(lái)完成的糙臼。一個(gè)插件可以支持多種功能庐镐,稱(chēng)之為目標(biāo)(goal),例如:compiler 插件有兩個(gè)目標(biāo):compile 和 testCompile变逃,分別實(shí)現(xiàn)編譯源代碼 和 編譯測(cè)試代碼必逆。
如何將插件與 Maven 的構(gòu)建生命周期綁定在一起呢?通過(guò)將插件的目標(biāo)(goal)與 build lifecycle 中 phase 綁定到一起,這樣名眉,當(dāng)要執(zhí)行某個(gè) phase 時(shí)粟矿,就調(diào)用插件來(lái)完成綁定的目標(biāo)。
如下圖所示:從圖中可以看出璧针,每一個(gè)階段可以綁定0 個(gè) 或 多個(gè)目標(biāo)嚷炉,每個(gè)插件可以提供 1 個(gè)或多個(gè)目標(biāo)。
4. 如何為自己的工程創(chuàng)建構(gòu)建生命周期
- 設(shè)置不同的 packaging 類(lèi)型
在 pom.xml 文件中探橱,packaging 類(lèi)型支持 jar, war, ear, pom 等多種類(lèi)型郑藏,不同的 packaging 類(lèi)型會(huì)使得不同的 phase 綁定不同的 plugin goal酬滤。下面是 packaging 類(lèi)型為 jar 時(shí)歹叮,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>胞枕,通過(guò)指定 goal 和 phase 來(lái)進(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. 我沒(méi)有在 pom.xml 指定任何 plugin腐泻,但是也能正常構(gòu)建工程
你可以能會(huì)疑問(wèn)决乎,默認(rèn)的 pom.xml 文件并沒(méi)有配置各種 plugin,但是也能正常構(gòu)建工程派桩?答案是 Maven 自己默認(rèn)指定了 plugin构诚。
下面是一個(gè)沒(méi)有配置任何 plugin 的 pom.xml,執(zhí)行 mvn install 的輸出日志铆惑,從日志中可以看到 一系列的 插件(plugin):版本號(hào):目標(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 入門(mén)指南
maven 生命周期
Maven 默認(rèn)插件以及功能
maven 依賴管理
maven-shade-plugin 入門(mén)指南
maven-assembly-plugin 入門(mén)指南
Introduction to the Build Lifecycle