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)建生命周期和插件