Maven的所有實際操作都是由插件完成的,如果沒有插件,Maven什麼都不會幹。
(即時你沒有在POM中配置<plugin>
元素,Super POM中也已經幫你引入了若干核心插件)
那麼問題來了,項目構建過程中,Maven是怎麼知道應該在什麼時候調用哪個插件的呢?
插件的調用時機,跟'生命週期'和'插件目標'有很大關係。
插件目標(Plugin Goal)是個什麼鬼
首先需要知道的是,Maven的所有具體任務都是交由插件實現的,而一個插件往往能實現若干個任務或功能。又或者可以說一個插件能實現若干個目標,比如編譯源碼、執行測試用例、打包項目等等。
簡單來說,在Maven中Plugin Goal可以看做是插件的一個功能。
Plugin Goal通常用‘插件前綴:插件目標‘的格式描述,常見的有:compiler:compile
,surefire:test
,dependency:tree
等。
所謂插件前綴就是插件的名稱的一個簡寫,比如compiler指的就是maven-complier-plugin這個插件。
compiler:compile
的意思既是maven-complier-plugin插件的compile目標。
構建生命週期(Build Lifecycle)又是什麼
構建生命週期是對構建過程的抽象和統一,包括了幾乎所有的構建步驟,如清理、初始化、編譯、測試、打包等等。
Maven爲不用的目的定義了3套相互獨立的生命週期:
- clean 用於清理項目
- default 用於構建項目
- site 用於構建項目站點
每種生命週期都是由若干階段(phase)組成,如clean生命週期由pre-clean,clean,post-clean三個階段組成。
階段之間是有序的,而且後面的階段依賴前面的階段。
用戶執行mvn clean
時,實際上maven會先執行pre-clean階段,然後再執行clean階段;執行mvn post-clean
時,則會依次執行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 |
插件目標與生命週期的綁定
插件目標是與生命週期的某個階段綁定的,比如maven-clean-plugin的clean目標與clean階段綁定,也就意味着當maven執行clean生命週期時,到達clean階段的時候,會調用maven-clean-plugin的clean目標(刪除項目輸出目錄)。
也就是說,插件目標的執行順序是由其綁定到的生命週期階段的順序決定的,如果多個插件目標綁定到同一個階段,那麼先聲明的插件會在後聲明的插件之前執行。
對於一些核心插件,Maven已經內置默認將一些插件目標綁定到了生命週期階段,特別是default生命週期,對於不同的打包類型(jar,war,pom等),Maven提供了不同的默認綁定。如下所示:
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 |
參考文獻
轉載請保留原文地址:Maven用戶都應該知道的一些事:構建生命週期和插件