Apache Maven --- [構建生命週期]

構建生命週期基礎html

Maven基於構建生命週期的核心概念.這意味着一個特定工件(項目)的構建和分佈過程是明肯定義的.咱們只須要學習一些必要的命令集合就能夠構建項目,而POM將會保證咱們獲得咱們想要的結果.java

這裏有三個內置的構建生命週期:default,clean,site.default支持項目部署,clean支持項目清理,而site支持你的項目站點文檔的建立.web

一個構建生命週期由階段構成apache

每個構建生命週期都被定義成不一樣的構建階段列表,每個構建階段都表明生命週期中的一個階段.安全

例如,default生命週期由如下階段組成bash

validate-驗證項目是正確的而且全部的必要信息是可用的服務器

compile-編譯項目源碼app

test-用合適的單元測試框架測試編譯後的代碼,這些測試不須要代碼被打包或部署框架

package-拿取編譯後的代碼並將它打包成它的可分配的格式,如JARmaven

verify-在集成測試結果上運行檢查以確保知足質量標準

install-安裝包到本地倉庫,做爲依賴提供給其餘本地項目

deploy-在構建環境下完成,拷貝最終的包到遠程倉庫分享給其餘的開發者和項目

這些生命週期階段(這裏並無顯示完整的生命週期階段)被連續執行以完成default生命週期.上述給出的生命週期階段意味着當default生命週期被使用時,Maven首先會驗證項目,而後嘗試編譯項目,執行單元測試,打包二進制文件(如:JAR),運行集成測試,檢查集成測試,安裝檢查後的包到本地倉庫,最後部署安裝包到遠程倉庫.

經常使用的命令行調用

在開發環境中,使用以下命令來構建和安裝工件到本地倉庫:

mvn install

這個命令會執行在install以前(包含install)default生命週期中的各個階段.你只須要調用執行最後的構建階段,在這裏就是install.

在構建環境中,使用如下命令來純淨的構建和部署工件到共享倉庫:

mvn clean deploy

這個命令同樣能夠被用在多模塊方案(一個項目擁有一個或多個子項目).Maven遍歷每一個子模塊並執行clean,deploy(包括全部在此以前的構建階段步驟).

一個構建階段由插件目標構成

然而,即便一個構建階段負責構建生命週期中的一個指定步驟,可是它執行這些責任的方式多是多種多樣的.這經過聲明插件目標綁定到那些構建階段來完成.

一個插件目標表明瞭一個指定的任務(比構建階段更精細),用來爲構建和管理項目作出貢獻.它額可能被綁定到零個或多個構建階段.一個沒有綁定到任何構建階段的插件目標能夠被從構建生命週期的外部直接調用執行.執行順序依賴插件目標和構建階段的調用順序.例如:考慮以下命令.clean和package是構建階段,而dependency:copy-dependencies是插件目標:

mvn clean dependency:copy-dependencies package

若是它被執行,clean構建階段首先被執行(意味着它將執行全部clean生命週期的前置階段,加上clean階段自己),而後執行dependency:copy-dependencies目標,最後執行package構建階段(全部default生命週期的前置階段加上package構建階段自己).

此外,若是目標被綁定到一個或多個構建階段,那麼它會被全部這些構建階段所調用.

另外,一個構建階段一樣能夠有零個或者多個插件目標綁定在上面.若是構建階段沒有目標綁定在上面,那麼這個構建階段將不會被執行.反之,則會執行綁定了的目標.

(請注意:在Maven2.0.5以上版本,多個目標綁定到一個階段按照它們在POM中被聲明的順序執行,可是不支持相同插件的多個實例.相同插件的多個實例分組依次執行在Maven2.0.11以上版本)

一些階段一般不從命令行調用

這些用連字符鏈接命名的階段(pre-*,post-*,或process-*)一般不從命令行直接調用.這些階段按順序排列構建,產生構建外部不可用的中間結果.在執行集成測試的狀況下,環境可能被掛機.

代碼覆蓋工具(如Jacoco)以及執行容器插件(如Tomcat,Cargo,Docker)綁定目標到pre-integration-test階段來準備集成測試容器環境.這些插件也綁定目標到post-integration-test階段來收集覆蓋統計或解除集成測試容器.

故障安全和代碼覆蓋插件綁定目標到integration-test和verify階段.獲得的結果是在verify以後測試和覆蓋報告是可用的.若是integration-test是從命令行被調用的,不會有報告生成.更壞的結果是測試容器環境將被掛起;Tomcat Web服務器和Docker實例會持續運行,並且Maven自身也不會終止.

使用構建生命週期設置你的項目

構建生命週期的使用很簡單,可是當你爲一個項目構造Maven構建時,你該如何分派任務到每個構建階段?

打包

首先,最經常使用的方式,是經過同名的POM元素來爲你的項目設置打包方案.一些可用的打包方案值是jar,war,ear和pom.若是沒有指定值,默認是jar.

每一個打包方案包含了一個目標列表綁定到特定方案.例如,jar打包方案將會綁定以下目標到default生命週期中的對應構建階段.

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

這基本上是綁定的標準集合.然而,一些打包方案以不一樣的方式處理它們.例如,一個純粹是元數據的項目(打包方案是pom)僅僅綁定目標到install階段和deploy階段.

注意爲了使某些打包類型可用,你也許須要包含一個指定的插件到你POM文件中的部分併爲該插件指定true.一個須要這麼作的插件案例是Plexus插件,它用來提供plexus-application和plexus-service打包類型.

插件

第二種添加目標到階段的方式是在你的項目中配置插件.插件是提供目標給Maven的工件.另外,一個插件也許有一個或者多個目標,其中每一個目標都表明了插件的一個功能.例如,編譯插件有兩個功能:compile和testCompile.前者編譯你的主要代碼的源碼,後者編譯你測試代碼的源碼.

正如你再接下來的章節將會看到的,插件包含能代表一個目標將會綁定到哪一個生命週期階段的信息.注意,添加插件的信息是不夠的,你還必須指定你想運行的目標做爲構建的一部分.

已經配置的目標將會被添加到根據打包類型所選擇的被綁定到生命週期的目標中.若是超過一個目標被綁定到一個特定的階段,那麼執行順序是自帶的優先於配置進POM的.注意你可使用元素來得到更多的順序控制.

例如,Modello插件默認綁定它的目標modello:java到generate-sources階段.(注意,這個目標生成java代碼).因此使用Modello插件,用它的模版來生成代碼並整合到構建中去.你應該添加如下內容到你POM的元素中的中去:

...
 <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>
       <goals>
         <goal>java</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
...

你也許奇怪爲何有元素在這.這是便於你在須要的時候能夠屢次運行相同的目標經過不一樣的配置.單獨的executions能夠給出一個ID以便於在繼承或者應用配置文件期間可讓你控制是否要合併目標配置或轉變爲一個額外的execution.

當給出許多executions匹配到一個特定的階段時,它們按在POM中指定的順序執行.繼承的execution優先執行.

如今,對於modello.java來講,它僅僅在generate-sources階段有意義.可是一些目標能夠被使用在超過一個階段中,並且默認狀況下它們也許是不能被察覺的.對於這些,你能夠本身指定階段.例如,讓咱們假設咱們有個目標display:time用來顯示當前時間到命令行,並且你但願它運行在process-test-resources階段來代表測試的開始時間.能夠像這樣配置:

...
 <plugin>
   <groupId>com.mycompany.example</groupId>
   <artifactId>display-maven-plugin</artifactId>
   <version>1.0</version>
   <executions>
     <execution>
       <phase>process-test-resources</phase>
       <goals>
         <goal>time</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
...

生命週期參考

下面羅列了default,clean和site生命週期的全部構建階段,它們是按序執行的:

Clean生命週期

pre-clean 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生命週期

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生命週期

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

內置的生命週期綁定

有些階段默認有一些目標綁定在上面.對於default生命週期來講,這些綁定依賴於打包類型值.這裏有一些綁定到階段的目標

Clean生命週期綁定

clean clean:clean

Default生命週期綁定-打包類型(ejb/ejb3/jar/par/rar/war)

process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
install install:install
deploy deploy:deploy

Default生命週期綁定-打包類型(ear)

generate-resources ear:generate-application-xml
process-resources resources:resources
package ear:ear
install install:install
deploy deploy:deploy

Default生命週期綁定-打包類型(maven-plugin)

generate-resources plugin:descriptor
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar and plugin:addPluginArtifactMetadata
install install:install
deploy deploy:deploy

Default生命週期綁定-打包類型(pom)

package site:attach-descriptor
install install:install
deploy deploy:deploy

Site生命週期綁定

site site:site
site-deploy site:deploy

參考

完整的Maven生命週期被定義在maven-core模塊的components.xml文件中,詳細介紹:https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html

在Maven2.x中,default生命週期綁定被包含在components.xml中,可是在Maven3.x,它們被定義在一個獨立的default-bindings.xml描述中.

相關文章
相關標籤/搜索