Maven的生命周期是抽象的,具體的操作由插件實現,類似於java的模板設計模式。
1、生命周期
- 認識生命周期
maven有clean、default、site三種生命周期,每種生命周期都包含一些階段,clean包含了pre-clean、clean、post-clean階段;default生命周期包含了validate、compile、test、package、verify、install、deploy階段;site生命周期包含了pre-site、site、post-site、site-deploy階段。三套生命周期是互相獨立的,每種生命周期的階段是前后依賴的。執行某個階段,則會先依次執行該生命周期的前面階段。
- 命令行執行生命周期
mvn clean 僅執行clean生命周期的pre-clean和clean階段
mvn test 執行default生命周期的validate、compile、test階段
mvn clean package 執行clean生命周期的pre-clean和clean階段以及default生命周期的validate、compile、test、package階段
mvn clean install site-deploy 執行三種生命周期
2、插件詳解
- 插件目標
maven中插件是以構件的形式存在。一個插件能完成好幾個功能,每個功能對應插件的一個目標。比如插件maven-compiler-plugin可以完成以下compile、testCompile目標
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\zhpli\Desktop\myapp\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\Users\zhpli\Desktop\myapp\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\zhpli\Desktop\myapp\target\test-classes [INFO]
- 插件綁定
maven生命周期與插件的某個目標綁定,執行具體的構建任務。比如compile生命周期與maven-compiler-plugin插件的compile目標綁定,執行編譯任務。
a、maven內置綁定插件
b、自定義綁定插件
用戶可以自己配置某個插件的某個目標綁定生命周期的某個階段,讓maven在構建項目時執行更多富有特色的任務。比如創建項目的源碼jar包,內置插件沒有涉及這一任務。maven-source-plugin插件的jar-no-fork目標能夠將項目的主代碼打包成jar文件,可以將該目標綁定到default生命周期的verify階段上,即在執行完成測試后和安裝構件之前創建源碼jar包。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.1</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- 插件配置
a、全局配置
該插件使用java1.8編譯,並生成jvm1.8兼容的字節碼文件。maven-compiler-plugin插件已經綁定的生命周期的階段均使用該配置。
<build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> 該插件的整體配置,各個目標均使用該配置 <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
b、局部配置(任務配置)
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> <executions> <execution> <id>ant-validate</id> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> 該插件目標的特有配置 <tasks> <echo>I am bound verify phase</echo> 輸出信息 </tasks> </configuration> </execution> <execution> <id>ant-verify</id> <phase>verify</phase> <goals> <goal>run</goal> </goals> <configuration> 該插件目標的特有配置 <tasks> <echo>I am bound verify phase</echo> 輸出信息 </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>
- 插件解析機制
與依賴構件一樣,插件構件同樣基於坐標存儲在Maven倉庫中。不同於依賴配置遠程倉庫使用的repotitories及其repository子元素,插件的遠程倉庫使用pluginRepositories和pluginRepositoryp配置,其他配置與依賴配置遠程倉庫一致
略略...