Chapter 10. 構建生命週期
10.1.1. 清理生命週期 (clean)
運行mvn clean將調用清理生命週期,它包含了三個生命週期階段:
-
pre-clean
-
clean
-
post-clean
在每個生命週期階段,你如果需要執行自己的任務或者通知可以使用插件maven-antrun-plugin
你也可以通過插件配置,改變原有生命週期階段的任務例如
Example 10.2. 自定義Clean插件的行爲
<project> <modelVersion>4.0.0</modelVersion> ... <build> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <directory>target-other</directory> <includes> <include>*.class</include> </includes> </fileset> </filesets> </configuration> </plugin> </plugins> </build> </project>
10.1.2. 默認生命週期 (default)
生命週期階段 | 描述 |
---|---|
validate | 驗證項目是否正確,以及所有爲了完整構建必要的信息是否可用 |
generate-sources | 生成所有需要包含在編譯過程中的源代碼 |
process-sources | 處理源代碼,比如過濾一些值 |
generate-resources | 生成所有需要包含在打包過程中的資源文件 |
process-resources | 複製並處理資源文件至目標目錄,準備打包 |
compile | 編譯項目的源代碼 |
process-classes | 後處理編譯生成的文件,例如對Java類進行字節碼增強(bytecode enhancement) |
generate-test-sources | 生成所有包含在測試編譯過程中的測試源碼 |
process-test-sources | 處理測試源碼,比如過濾一些值 |
generate-test-resources | 生成測試需要的資源文件 |
process-test-resources | 複製並處理測試資源文件至測試目標目錄 |
test-compile | 編譯測試源碼至測試目標目錄 |
test | 使用合適的單元測試框架運行測試。這些測試應該不需要代碼被打包或發佈 |
prepare-package | 在真正的打包之前,執行一些準備打包必要的操作。這通常會產生一個包的展開的處理過的版本(將會在Maven 2.1+中實現) |
package | 將編譯好的代碼打包成可分發的格式,如JAR,WAR,或者EAR |
pre-integration-test | 執行一些在集成測試運行之前需要的動作。如建立集成測試需要的環境 |
integration-test | 如果有必要的話,處理包併發布至集成測試可以運行的環境 |
post-integration-test | 執行一些在集成測試運行之後需要的動作。如清理集成測試環境。 |
verify | 執行所有檢查,驗證包是有效的,符合質量規範 |
install | 安裝包至本地倉庫,以備本地的其它項目作爲依賴使用 |
deploy | 複製最終的包至遠程倉庫,共享給其它開發人員和項目(通常和一次正式的發佈相關) |
10.1.3. 站點生命週期 (site)
Maven不僅僅能從一個項目構建軟件構件,它還能爲一個或者一組項目生成項目文檔和報告。項目文檔和站點生成有一個專有的生命週期,它包含了四個階段:
-
pre-site
-
site
-
post-site
-
site-deploy
mvn site
10.2. 打包相關生命週期
綁定到每個階段的特定目標默認根據項目的打包類型設置。一個打包類型爲jar
的項目和一個打包類型爲war
的項目擁有不同的兩組默認目標。
常用的打包類型,jar,pom,manven-plugin,EJB,WAR,EAR.也可以使用插件支持其他項目的打包,例如swf
10.3. 通用生命週期目標
Chapter 11. 構建Profile
11.1. Profile是用來做什麼的?
Profile能讓你爲一個特殊的環境自定義一個特殊的構建;profile使得不同環境間構建的可移植性成爲可能。
構建可移植性!!!!!!!!!
一個構建的“可移植性”是指將一個項目在不同的環境中構建的難易度。
11.1.1.2. 環境可移植性!!!!
一個profile 幾乎可以覆蓋項目模型的所有內容
<project> <profiles> <profile> <build> <defaultGoal>...</defaultGoal> <finalName>...</finalName> <resources>...</resources> <testResources>...</testResources> <plugins>...</plugins> </build> <reporting>...</reporting> <modules>...</modules> <dependencies>...</dependencies> <dependencyManagement>...</dependencyManagement> <distributionManagement>...</distributionManagement> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <properties>...</properties> </profile> </profiles> </project>