Maven用戶都應(yīng)該知道的一些事:構(gòu)建生命周期和插件

Maven的所有實際操作都是由插件完成的,如果沒有插件,Maven什么都不會干。
(即時你沒有在POM中配置<plugin>元素,Super POM中也已經(jīng)幫你引入了若干核心插件)

那么問題來了,項目構(gòu)建過程中,Maven是怎么知道應(yīng)該在什么時候調(diào)用哪個插件的呢?

插件的調(diào)用時機(jī),跟'生命周期'和'插件目標(biāo)'有很大關(guān)系。

插件目標(biāo)(Plugin Goal)是個什么鬼

首先需要知道的是,Maven的所有具體任務(wù)都是交由插件實現(xiàn)的,而一個插件往往能實現(xiàn)若干個任務(wù)或功能。又或者可以說一個插件能實現(xiàn)若干個目標(biāo),比如編譯源碼、執(zhí)行測試用例、打包項目等等。
簡單來說,在Maven中Plugin Goal可以看做是插件的一個功能。

Plugin Goal通常用‘插件前綴:插件目標(biāo)‘的格式描述,常見的有:compiler:compile,surefire:test,dependency:tree等。

所謂插件前綴就是插件的名稱的一個簡寫,比如compiler指的就是maven-complier-plugin這個插件。
compiler:compile的意思既是maven-complier-plugin插件的compile目標(biāo)。

構(gòu)建生命周期(Build Lifecycle)又是什么

構(gòu)建生命周期是對構(gòu)建過程的抽象和統(tǒng)一,包括了幾乎所有的構(gòu)建步驟,如清理、初始化、編譯、測試、打包等等。

Maven為不用的目的定義了3套相互獨立的生命周期:

  • clean 用于清理項目
  • default 用于構(gòu)建項目
  • site 用于構(gòu)建項目站點

每種生命周期都是由若干階段(phase)組成,如clean生命周期由pre-clean,clean,post-clean三個階段組成。

階段之間是有序的,而且后面的階段依賴前面的階段。
用戶執(zhí)行mvn clean時,實際上maven會先執(zhí)行pre-clean階段,然后再執(zhí)行clean階段;執(zhí)行mvn post-clean時,則會依次執(zhí)行pre-clean,clean,post-clean三個階段。

3個生命周期各自的階段如下:

Clean Lifecycle

Phase Description
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 Lifecycle

Phase Description
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 Description
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

插件目標(biāo)與生命周期的綁定

插件目標(biāo)是與生命周期的某個階段綁定的,比如maven-clean-plugin的clean目標(biāo)與clean階段綁定,也就意味著當(dāng)maven執(zhí)行clean生命周期時,到達(dá)clean階段的時候,會調(diào)用maven-clean-plugin的clean目標(biāo)(刪除項目輸出目錄)。

也就是說,插件目標(biāo)的執(zhí)行順序是由其綁定到的生命周期階段的順序決定的,如果多個插件目標(biāo)綁定到同一個階段,那么先聲明的插件會在后聲明的插件之前執(zhí)行。

對于一些核心插件,Maven已經(jīng)內(nèi)置默認(rèn)將一些插件目標(biāo)綁定到了生命周期階段,特別是default生命周期,對于不同的打包類型(jar,war,pom等),Maven提供了不同的默認(rèn)綁定。如下所示:

Clean Lifecycle Bindings

Phase plugin:goal
clean clean:clean

Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

Phase plugin:goal
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 Lifecycle Bindings - Packaging ear

Phase plugin:goal
generate-resources ear:generate-application-xml
process-resources resources:resources
package ear:ear
install install:install
deploy deploy:deploy

Default Lifecycle Bindings - Packaging maven-plugin

Phase plugin:goal
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 Lifecycle Bindings - Packaging pom

Phase plugin:goal
package
install install:install
deploy deploy:deploy

Site Lifecycle Bindings

Phase plugin:goal
site site:site
site-deploy site:deploy

參考文獻(xiàn)

轉(zhuǎn)載請保留原文地址:Maven用戶都應(yīng)該知道的一些事:構(gòu)建生命周期和插件

?著作權(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)容

  • 生命周期是maven的又一大核心,maven的生命周期是抽象的,而實際行為都是以插件的方式來完成的,下面我將對生命...
    小煉君閱讀 1,312評論 0 50
  • Maven聚合模塊: 因為Maven是提倡模塊化編程的,所以會以多個工程分為多個模塊。如果所有的功能、模塊都寫在一...
    凱哥學(xué)堂閱讀 827評論 0 4
  • 什么是構(gòu)建生命周期 Maven的構(gòu)建過程被分解為構(gòu)建生命周期、階段和目標(biāo)。一個構(gòu)建周期由一系列的構(gòu)建階段組成,每一...
    歐余山南閱讀 962評論 0 0
  • 1. Maven 構(gòu)建生命周期 Maven 構(gòu)建生命周期就是 Maven 將一個整體任務(wù)劃分為一個個的階段,類似于...
    data4閱讀 31,381評論 3 35
  • 感覺是水逆了 最近日子難過的一批 會突然的流淚 突然的心塞 突然的難受 自己都沒想過會這么敏感脆弱 我是個經(jīng)常自我...
    擁彼閱讀 289評論 0 0

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