詳解pom.xml中maven profile的激活方式
maven profiles簡(jiǎn)單介紹:
maven profiles是maven從POM4.0開(kāi)始給我們提供的一種新的特性。它允許maven根據(jù)不同的環(huán)境采用不同的maven配置。一個(gè)profiles標(biāo)簽中可以有很多個(gè)profile,只需要根據(jù)不同的項(xiàng)目環(huán)境,激活不同的profile即可。
提示:也可以同時(shí)激活多個(gè)profile。
profiles的組成(示例):

注:使用maven的profile功能,我們可以實(shí)現(xiàn)多環(huán)境配置文件的動(dòng)態(tài)切換。
maven profile的激活方式:
方式一:使用mvn ... -P xxx指令,指定激活id為xxx的profile。
第一步:在pom.xml中編寫(xiě)profiles。
<!-- 使用mvn ... -P xxx指令,指定激活id為xxx的profile -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<!-- profile唯一id -->
<id>test-id</id>
<!-- 引入fastjson依賴(lài) -->
<dependencies>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<!-- profile唯一id -->
<id>prod-id</id>
<!-- 引入lombok依賴(lài) -->
<dependencies>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>第二步:打包時(shí)通過(guò)maven指令,指定激活哪一個(gè)profile。

如果需要激活多個(gè)profile的話,使用逗號(hào)隔開(kāi)多個(gè)profile的id即可:

方式二:在profile里面使用activation標(biāo)簽,當(dāng)滿足條件時(shí)激活該profile。
提示:activation標(biāo)簽,允許我們定義默認(rèn)激活配置、定義條件激活(滿足什么條件就進(jìn)行激活)配置,下面一一介紹。
activeByDefault默認(rèn)激活:
<!-- ************** 設(shè)置默認(rèn)激活 ************** -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test-id</id>
<activation>
<!-- 設(shè)置默認(rèn)激活 -->
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod-id</id>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>注:默認(rèn)激活級(jí)別最低!一旦其它某個(gè)激活指令(mvn指令激活或activation的其他條件激活)生效時(shí),那么默認(rèn)激活指令就不會(huì)生效。
根據(jù)jdk激活:
<!-- ************** 根據(jù)jdk激活 ************** -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test-id</id>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod-id</id>
<activation>
<!--
當(dāng)jdk的版本號(hào)是以下面的值開(kāi)頭時(shí), 激活此profile
即:當(dāng)jdk的版本號(hào)是startWith下面的值時(shí),激活此profile
-->
<jdk>1.8.0_</jdk>
<!--
還可以取反,當(dāng)jdk的版本號(hào) 不是 startWith下面的值時(shí),激活此配置
-->
<!-- <jdk>!1.8</jdk> -->
</activation>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>根據(jù)os激活:
<!-- ************** 根據(jù)os激活 ************** -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test-id</id>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod-id</id>
<activation>
<!--
當(dāng)操作系統(tǒng)滿足下述所有條件時(shí),激活
注: 如果有多個(gè)條件,那么必須所有條件都滿足時(shí),才會(huì)激活對(duì)應(yīng)的profile
-->
<os>
<!-- 操作系統(tǒng)名,如【W(wǎng)indows 10】 -->
<name>Windows 10</name>
<!-- 操作系統(tǒng)隸屬, 如【windows】、【unix】 -->
<family>windows</family>
<!-- 操作系統(tǒng)的體系結(jié)構(gòu),如【amd64】 -->
<arch>amd64</arch>
<!-- 操作系統(tǒng)版本號(hào),如【10.0】 -->
<version>10.0</version>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>注:對(duì)于os的基本信息,我們可以這樣獲取:
/**
* 獲取操作系統(tǒng)信息
*
* @date 2019/9/4 11:21
*/
private static void osInfo() {
Properties osProperties = System.getProperties();
// name-操作系統(tǒng)名,如【W(wǎng)indows 10】
System.out.println(osProperties.get("os.name"));
// family-操作系統(tǒng)隸屬, 如【windows】、【unix】, 可根據(jù)name來(lái)獲取
System.out.println(
osProperties.get("os.name").toString().startsWith("Window") ? "windows" : "unix"
);
// arch-操作系統(tǒng)的體系結(jié)構(gòu),如【amd64】
System.out.println(osProperties.get("os.arch"));
// version-操作系統(tǒng)版本號(hào),如【10.0】
System.out.println(osProperties.get("os.version"));
}根據(jù)file存在與否激活:
<!-- ************** 根據(jù)file存在與否激活 ************** -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test-id</id>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod-id</id>
<!--
根據(jù)文件存在與否, 來(lái)激活profile
注:文件路徑可以是絕對(duì)路徑,也可以是相對(duì)路徑(相對(duì)pom.xml的路徑)。
注:在exists標(biāo)簽里,如果寫(xiě)絕對(duì)路徑,不要使用${project.basedir}或
${pom.basedir};經(jīng)本人測(cè)試,在exists標(biāo)簽里使用${}取不到值。
注:missing與exists最好不要同時(shí)使用。 如果同時(shí)使用的話,missing就
會(huì)形同虛設(shè),是否激活此profile完全由exists決定,。
-->
<activation>
<file>
<!-- 相對(duì)路徑示例 -->
<exists>src/main/resources/xyza.yml</exists>
<!-- <missing>src/main/resources/abcd.yml</missing> -->
<!-- 絕對(duì)路徑示例 -->
<!-- <exists>C:/Users/JustryDeng/Desktop/zx-lw.log</exists> -->
<!-- <missing>/usr/apps/file/info.properties</missing> -->
</file>
</activation>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>根據(jù)maven指令-D參數(shù)激活:
<!-- ************** 根據(jù) maven指令參數(shù)-D 激活 ************** -->
<profiles>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test-id</id>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod-id</id>
<activation>
<!--
maven指令參數(shù)-D激活
注:與根據(jù)maven指令參數(shù)-P 指定profile的id進(jìn)行激活類(lèi)似
-->
<property>
<name>pk</name>
<value>pv</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>打包時(shí)通過(guò)maven指令-D,指定激活哪一個(gè)profile:

如果想激活多個(gè)profile,可以這樣:

提示:除了在項(xiàng)目的pom.xml中可以對(duì)profile進(jìn)行配置激活外,還可以在當(dāng)前用戶(hù)下的.m2/settings.xml文件里,對(duì)profile進(jìn)行配置激活;還可以在maven安裝目錄下的conf/settings.xml文件里,對(duì)profile進(jìn)行配置激活。上述三者的區(qū)別是:在項(xiàng)目的pom.xml中配置profile,那么配置的profile只對(duì)該項(xiàng)目有效;在用戶(hù)下的.m2/settings.xml文件里配置profile,那么配置的profile只對(duì)該用戶(hù)下的(所有)項(xiàng)目有效;在maven安裝目錄下的conf/settings.xml文件里配置profile,那么配置的profile對(duì)使用該maven程序的(所有)項(xiàng)目有效。
到此這篇關(guān)于詳解pom.xml中maven profile的激活方式的文章就介紹到這了,更多相關(guān)Maven profile激活方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot手動(dòng)開(kāi)啟事務(wù):DataSourceTransactionManager問(wèn)題
這篇文章主要介紹了SpringBoot手動(dòng)開(kāi)啟事務(wù):DataSourceTransactionManager問(wèn)題,具有很好的價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
使用SpringBoot的CommandLineRunner遇到的坑及解決
這篇文章主要介紹了使用SpringBoot的CommandLineRunner遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Java實(shí)戰(zhàn)之校園外賣(mài)點(diǎn)餐系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡(jiǎn)易的校園外賣(mài)點(diǎn)餐系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis 等,感興趣的可以了解一下2022-03-03
Java使用wait() notify()方法操作共享資源詳解
這篇文章主要為大家詳細(xì)介紹了Java使用wait() notify()方法操作共享資源,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Java Spring5學(xué)習(xí)之JdbcTemplate詳解
這篇文章主要介紹了Java Spring5學(xué)習(xí)之JdbcTemplate詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
JMagick實(shí)現(xiàn)基本圖像處理的類(lèi)實(shí)例
這篇文章主要介紹了JMagick實(shí)現(xiàn)基本圖像處理的類(lèi),實(shí)例分析了java圖像處理的相關(guān)技巧,需要的朋友可以參考下2015-06-06
在idea中將創(chuàng)建的java web項(xiàng)目部署到Tomcat中的過(guò)程圖文詳解
這篇文章主要介紹了在idea中將創(chuàng)建的java web項(xiàng)目部署到Tomcat中的過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

