概念
- lifecycle 生命周期
- phases 階段噪裕。生命周期由多個階段組成
- goals 目標募闲。每個階段都可以有多個目標被執(zhí)行
Maven有3個內(nèi)置生命周期
- default 處理項目構(gòu)建和部署
- clean 清除Maven產(chǎn)生的文件和目錄
- site 處理項目文檔的創(chuàng)建
備注
- Maven以被指定的階段(phase)為基礎(chǔ)推斷使用的生命周期(lifecycle)肴熏。如==package==階段表明使用==default==生命周期忽孽。
- 每個階段(phase)由多個插件目標(plugin goals)組成。一個插件目標是一個特定的用來構(gòu)建項目的任務盯另。有一些目標只在特定的階段起作用(例如:Compiler插件的的compile目標只在compile階段起作用性含,但是CheckStyle插件的checkstyle目標可以在任何階段運行)。所以有一些目標是被綁定到特定的生命周期的階段的鸳惯。
生命周期的重要階段
clean生命周期
- clean階段 移除所有由Maven在構(gòu)建階段創(chuàng)建的文件和目錄商蕴。
site生命周期
- site階段 產(chǎn)生項目的文檔
default生命周期
- validate階段 校驗所有項目信息是否存在和正確
- process-resources階段 復制項目resources到目標目錄中打包
- compile階段 編譯源碼
- test階段 運行單元測試
- integration-test階段 在集成測試環(huán)境中處理打包
- verify階段 運行校驗來檢驗打包的有效性
- install階段 將打好的包發(fā)布到本地倉庫
- deploy階段 將打好的包發(fā)布到配置的倉庫中
關(guān)于階段、插件和目標的表格
階段 | 插件 | 目標 |
---|---|---|
clean | Maven Clean plugin | clean |
site | Maven Site plugin | site |
process-resources | Maven Resources plugin | resource |
compile | Maven Compiler plugin | compile |
test | Maven Surefire plugin | test |
package | Varies based on the packaging; for instance, the Maven JAR plugin | jar(in the case of a Maven JAR plugin) |
install | Maven install plugin | install |
deploy | Maven deploy plugin | deploy |
配置文件
Maven提供了三種類型的配置文件
- 項目配置文件:定義在項目的pom文件中
- 用戶配置文件:定義在用戶settings文件中(用戶HOME路徑的.m2子路徑中)
- 全局配置文件:定義在全局settings文件中(M2_HOME的conf路徑中)
配置文件屬性
pom.xml中
<profile>
<id>test</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
settings.xml中
<profile>
<id>test</id>
<activation>...</activation>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<properties>…</properties>
</profile>
Profiles激活方式
- 顯性激活
mvn -P dev package
- 通過settings
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
- 依賴于環(huán)境變量激活
<profile>
<activation>
<property>
<name>debug</name>
</property>
</activation>
...
</profile>
如果系統(tǒng)屬性debug被定義并且有值芝发,這個 profile會被激活究恤。
- 依賴于OS設(shè)置
<profile>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
...
</profile>
此配置在Windows系統(tǒng)中會被激活。
- 存在或者缺失某個文件
<profile>
<activation>
<file>
<missing>target/site</missing>
</file>
</activation>
</profile>
如果target/site文件缺失后德,此配置將會激活部宿。
屬性
屬性類型
- 環(huán)境變量(Environment variables):帶有前綴env.的變量,將返回shell的環(huán)境變量值。如${env.PATH}將返回PATH變量理张。
- pom變量:帶有前綴project.的變量赫蛇,將返回pom文件中元素的值。如${project.version}將返回pom文件中<version>標簽的值雾叭。
- settings變量:帶有前綴settings.的變量悟耘,將返回settings文件中元素的值。如${settings.offline}將返回settings中<offline>標簽的值织狐。
- Java屬性:在Java中任何可以通過System.getProperties()方法訪問的屬性暂幼。如${java.home}。
- 正常屬性:在<properties>標簽中定于的屬性移迫。