詳解Maven profile配置管理及激活profile的幾種方式
為了實(shí)現(xiàn)不同環(huán)境構(gòu)建的不同需求,這里使用到了 profile。因?yàn)?profile 能夠在構(gòu)建時(shí)修改 pom 的一個(gè)子集,或者添加額外的配置元素。接下來(lái)介紹 Maven 中對(duì) profile 的配置和激活。
針對(duì)不同環(huán)境的 profile 的配置
為了體現(xiàn)不同環(huán)境的不同構(gòu)建,需要配置好不同環(huán)境的 profile,代碼如下:
<profiles> <profile> <id>dev_evn</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql://localhost:3306/test</db.url> <db.username>root</db.username> <db.password>root</db.password> </properties> </profile> <profile> <id>test_evn</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql://localhost:3306/test_db</db.url> <db.username>root</db.username> <db.password>root</db.password> </properties> </profile> </profiles>
在兩個(gè)不同的 profile 中,配置了同樣的屬性,不一樣的值。按照前面的介紹,在開(kāi)發(fā)時(shí)可以用 mvn 命令后面添加“-Pdev_evn”激活“dev_evn profile”。
激活 profile 配置
在 Maven 中,可以選用如下的方式激活 profile。
1. 命令行激活
用戶可以在 mvn 命令行中添加參數(shù)“-P”,指定要激活的 profile 的 id。如果一次要激活多個(gè) profile,可以用逗號(hào)分開(kāi)一起激活。例如:
mvn clean install -Pdev_env,test_evn
這個(gè)命令就同時(shí)激活了 id 為“dev_evn”和“test_evn”的兩個(gè) profile。
2. Settings 文件顯示激活
如果希望某個(gè) profile 默認(rèn)一直處于激活狀態(tài),可以在 settings.xml 中配置 activeProfiles 元素,指定某個(gè) profile 為默認(rèn)激活狀態(tài),樣例配置代碼如下:
<settings> ... <activeProfiles> <activeProfile>dev_evn</activeProfile> </activeProfiles> ... </settings>
3. 系統(tǒng)屬性激活
可以配置當(dāng)某個(gè)系統(tǒng)屬性存在時(shí)激活 profile,代碼如下:
<profiles> <profile> ... <activation> <property> <name>profileProperty</name> </property> </activation> </profile> </profiles>
甚至還可以進(jìn)一步配置某個(gè)屬性的值是什么時(shí)候激活,例如:
<profiles> <profile> ... <activation> <property> <name>profileProperty</name> <value>dev</value> </property> </activation> </profile> </profiles>
這樣就可以在 mvn 中用“-D”參數(shù)來(lái)指定激活,例如:
Mvn clean install -DprofileProperty=dev
表示激活屬性名稱(chēng)為 profileProperty,值為 dev 的 profile。
實(shí)際上這也是一種命令激活 profile 的方法,只是用的是“-D”參數(shù)指定激活的屬性和值,而前面的是用的“-P”參數(shù)指定激活的 profile 的 id 而已。
4. 操作系統(tǒng)環(huán)境激活
用戶可以通過(guò)配置指定不同操作系統(tǒng)的信息,實(shí)現(xiàn)不同操作系統(tǒng)做不同的構(gòu)建。例如:
<profiles> <profile> <activation> <os> <name>Window XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> </profile> </profiles>
family 的值是 Windows、UNIX 或 Mac。name 為操作系統(tǒng)名稱(chēng)。arch為操作系統(tǒng)的架構(gòu)。version為操作系統(tǒng)的版本。具體的值可以通過(guò)查看環(huán)境中的系統(tǒng)屬性“os.name”“os.arch”和“os.version”獲取。
5. 文件存在與否激活
當(dāng)然,也可以通過(guò)配置判斷某個(gè)文件存在與否來(lái)決定是否激活 profile,樣例配置代碼如下:
<profiles> <profile> <activation> <file> <missing>t1.properties</missing> <exists>t2.properties</exists> </file> </activation> </profile> </profiles>
6. 默認(rèn)激活
最后,還可以配置一個(gè)默認(rèn)的激活 profile,例如:
<profiles> <profile> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles>
需要注意的是,如果 pom 中有任何一個(gè) profile 通過(guò)其他方式被激活的話,所有配置成默認(rèn)激活的 profile 都會(huì)自動(dòng)失效。 可以使用如下命令查看當(dāng)前激活的 profile。
Mvn help:active-profiles
也可以使用如下命令查看所有的 profile。
Mvn help:all-profiles
profile 的種類(lèi)
前面介紹了 profile 的意義和激活方式。那么在 Maven 中,有哪些 profile?如何配置呢?
根據(jù) profile 配置的位置不同,可以將 profile 分成如下幾種。
1)pom.xml
pom.xml 中聲明的 profile 只對(duì)當(dāng)前項(xiàng)目有效。
2)用戶 settings.xml
在用戶目錄下的“.m2/settings.xml”中的 profile,對(duì)本機(jī)上的該用戶的所有 Maven 項(xiàng)目有效。
3)全局 settings.xml
在 Maven 安裝目錄下 conf/settings.xml 中配置的 profile,對(duì)本機(jī)上所有項(xiàng)目都有效。
為了不影響其他用戶且方便升級(jí) Maven,一般配置自己的 settings.xml,不要輕易修改全局的 settings.xml。同樣的道理,一般不需要修改全局 settings.xml 中的 profile。
不同類(lèi)型的 profile 中可以聲明的 pom 元素是不一樣的,pom.xml 中的 profile 能夠隨同 pom.xml 一起提交到代碼倉(cāng)庫(kù)中,被 Maven 安裝到本地倉(cāng)庫(kù)里面,并且能被部署到遠(yuǎn)程 Maven 倉(cāng)庫(kù)中。也就是說(shuō),可以保證 profile 伴隨特定的 pom.xml 一起存在。所以它可以修改或者添加很多 pom 元素,例如:
<project> <repositories></repositories> <pluginRepositories></pluginRepositories> <dependencies></dependencies> <dependencyManagement></dependencyManagement> <modules></modules> <properties></properties> <reporting></reporting> <build> <plugins></plugins> <defaultGoal></defaultGoal> <resources></resources> <testResources></testResources> <finalName></finalName> </build> </project>
如上代碼所示,在 pom 中的 profile 元素比較多,可以添加或修改插件配置、項(xiàng)目資源目錄、測(cè)試資源目錄配置和項(xiàng)目構(gòu)建的默認(rèn)名稱(chēng)等。
除了 pom 中的 profile 外,其他外部的 profile 可以配置的元素相對(duì)就少些,因?yàn)槟切┩獠?profile 無(wú)法保證同項(xiàng)目中的 pom.xml 一起發(fā)布。
如果在外部 profile 中配置了項(xiàng)目依賴(lài),開(kāi)發(fā)用戶可以在本地編譯,但是因?yàn)橐蕾?lài)配置沒(méi)有隨同 pom.xml 一起發(fā)布部署到倉(cāng)庫(kù)中,別的用戶下載了該項(xiàng)目后,就會(huì)因?yàn)槿鄙僖蕾?lài)而失敗。
為了避免這樣的不一致情況,很多在 pom 的 profile 可以出現(xiàn)的元素不允許在外部 profile 中出現(xiàn)。
在外部 profile 可以聲明的元素如下:
<project> <repositories></repositories> <pluginRepositories></pluginRepositories> <properties></properties> </project>
這些外部 profile 元素不足以影響項(xiàng)目的正常構(gòu)建,只會(huì)影響項(xiàng)目的倉(cāng)庫(kù)和 Maven 屬性。
到此這篇關(guān)于詳解Maven profile配置管理及激活profile的幾種方式的文章就介紹到這了,更多相關(guān)Maven profile配置及激活profile內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- maven profile自動(dòng)切換環(huán)境參數(shù)的2種方法詳解
- 使用maven profile指定配置文件打包適用多環(huán)境的方法
- Maven管理SpringBoot Profile詳解
- maven的pom.xml中profiles的作用詳解
- maven多profile 打包下 -P參和-D參數(shù)的實(shí)現(xiàn)
- Maven 多profile及指定編譯問(wèn)題的解決
- 詳解maven中profiles使用實(shí)現(xiàn)
- maven profile實(shí)現(xiàn)多環(huán)境配置的示例
- maven profile動(dòng)態(tài)選擇配置文件詳解
- Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐
- maven中profile的使用
相關(guān)文章
java并發(fā)編程專(zhuān)題(四)----淺談(JUC)Lock鎖
這篇文章主要介紹了java并發(fā)編程(JUC)Lock鎖的相關(guān)內(nèi)容,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Android仿微信實(shí)現(xiàn)左滑顯示刪除按鈕功能
這篇文章主要為大家詳細(xì)介紹了java仿微信實(shí)現(xiàn)左滑顯示刪除按鈕功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10SpringBoot定時(shí)任務(wù)參數(shù)運(yùn)行代碼實(shí)例解析
這篇文章主要介紹了SpringBoot定時(shí)任務(wù)運(yùn)行代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(58)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08