Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐
前言
目前,企業(yè)項(xiàng)目的開發(fā)過程中,往往會(huì)使用配置文件來(lái)做一些配置項(xiàng)來(lái)實(shí)現(xiàn)項(xiàng)目部署的靈活性,避免硬編碼的方式在環(huán)境變化時(shí)需要對(duì)代碼進(jìn)行重新編譯。但是往往在項(xiàng)目周期中存在著各種環(huán)境:如開發(fā)環(huán)境、測(cè)試環(huán)境以及生產(chǎn)環(huán)境等,而且在不同的運(yùn)行環(huán)境中可能牽扯到大量的配置項(xiàng)變化。如果在不同的部署環(huán)境中切換,對(duì)配置文件的管理往往容易讓程序員感覺非常的混亂。
為了避免這種換亂,研發(fā)過程中也有比較多的手段進(jìn)行。比如,有公司就采用VPN的虛擬網(wǎng)絡(luò)環(huán)境,讓測(cè)試環(huán)境、生產(chǎn)環(huán)境的網(wǎng)絡(luò)一致,讓程序員在不同環(huán)境中對(duì)版本進(jìn)行發(fā)布時(shí)只需要對(duì)VPN進(jìn)行切換即可。以免發(fā)生網(wǎng)絡(luò)配置項(xiàng)改錯(cuò),漏改等現(xiàn)象的發(fā)生。這樣個(gè)人覺得還不錯(cuò),唯一有一點(diǎn)句是調(diào)整網(wǎng)絡(luò)環(huán)境、設(shè)備環(huán)境的成本應(yīng)該也比較高。
當(dāng)然profile的方式應(yīng)該算是比較經(jīng)濟(jì)的。我知道的比如spring-boot、maven都可以支持到profile的方式來(lái)對(duì)不同環(huán)境進(jìn)行指定。本文希望介紹一下,我理解的使用maven的profile方式來(lái)進(jìn)行不同環(huán)境切換。講得不到位的地方希望看官嘴下留情,也多指定。
Maven 的 profile:
在maven的 pom.xml
文件中有一個(gè)配置項(xiàng)叫著profiles
節(jié)點(diǎn),如下:
<profiles> <profile> <id>test</id> <properties> <active.profile>test</active.profile> <jdbc.url>127.0.0.1</jdbc.url> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <profile> <id>develop</id> <properties> <active.profile>develop</active.profile> <jdbc.url>192.168.1.102</jdbc.url> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>product</id> <properties> <actived.profile>product</actived.profile> <jdbc.url>10.21.41.100</jdbc.url> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> </profiles>
其中profiles
節(jié)點(diǎn)下可以填寫多個(gè)profile
其中profile
主要包含了三個(gè)屬性id
、properties
、activation
。
id
應(yīng)該是為了區(qū)分profile
用的。
properties
就是對(duì)應(yīng)的屬性。
activation
應(yīng)該是主要用來(lái)指定是否被默認(rèn)激活的,它還有一個(gè)子節(jié)點(diǎn)activeByDefault
, 如果子節(jié)點(diǎn)activeByDefault
內(nèi)的值為true表示他會(huì)被激活。它還有一些子節(jié)點(diǎn),但是不知道什么用。后續(xù)看看在學(xué)習(xí)下。
實(shí)踐前期準(zhǔn)備
我準(zhǔn)備建立一個(gè)簡(jiǎn)單的maven功能來(lái)實(shí)踐一下maven的profile
實(shí)現(xiàn)不同的配置管理,所以首先需要建議一個(gè)maven工程。本文采用的是idea進(jìn)行試驗(yàn)的。一下是我建立工程的結(jié)構(gòu)圖。
由于我只是需要對(duì)配置文件進(jìn)行管理,所以是完全不需要建立任何java類就可以的。
- 首先建立了三個(gè)
profile
相關(guān)的配置文件develop.properties
、product.properties
、test.properties
- 在三個(gè)文件中我就只寫了一個(gè)屬性
app.name
,三個(gè)文件中分別為develop.maven.profile
、product.maven.profile
、test.maven.profile
. - 為了驗(yàn)證在各類配置文件中都是可行的, 我建立了兩個(gè)供測(cè)試的配置文件
application.properties
、application.xml
.
application.properties 的內(nèi)容為:
application.xml的內(nèi)容為:
實(shí)踐一:
實(shí)踐一主要采用profile
+ filter
的方式實(shí)現(xiàn)內(nèi)容的注入。
該方式的思想是通過 filter下的文件編寫可變動(dòng)的配置項(xiàng),由filters
標(biāo)簽引入不同的配置文件項(xiàng),然后提取不同的配置文件中的配置項(xiàng)填充到resources
下的配置文件中。
所以其中的關(guān)鍵標(biāo)簽包含了 profile
filter
resource
Maven的配置文件pom.xml
的內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>maven-test</artifactId> <groupId>com.maomao.maven</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven-profile</artifactId> <version>1.0-SNAPSHOT</version> <profiles> <profile> <id>test</id> <properties> <active.profile>test</active.profile> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <profile> <id>develop</id> <properties> <active.profile>develop</active.profile> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>product</id> <properties> <actived.profile>product</actived.profile> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> </profiles> <build> <filters> <filter>src/filters/${active.profile}.properties</filter> </filters> <resources> <resource> <!--一定要讓filtering為true 否則無(wú)法對(duì)內(nèi)容進(jìn)行注入--> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
執(zhí)行maven命令進(jìn)行打包:
mvn clean install
執(zhí)行后打包結(jié)果targets
文件夾下的配置文件application.properties
、 application.xml
的占位置被填充。
當(dāng)然如果切換profiles
下的激活項(xiàng),填充的內(nèi)容自然也會(huì)發(fā)生變化?;蛘卟捎胢aven命令進(jìn)行激活項(xiàng)的切換:
mvn clean package -Ptest # 指定激活項(xiàng)的ID
實(shí)踐二:
實(shí)踐二主要采用profile
直接將屬性配置到了profile
下的properties
節(jié)點(diǎn)下。此方法就不在需要多余的filter
properties文件配置了。
例如:
<profile> <id>product</id> <properties> <actived.profile>product</actived.profile> <jdbc.url>10.21.41.100</jdbc.url> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile>
這里properites
中多了一個(gè)jdbc.url
屬性。那我們的application.properties
文件中同樣適用兩個(gè)占位符
app.name=${app.name} jdbc.url=${jdbc.url}
同樣執(zhí)行maven
命令進(jìn)行打包:
mvn clean install -Pproduct
打包之后的 application.properties
內(nèi)容對(duì)應(yīng)變?yōu)?/p>
結(jié)束語(yǔ)
整個(gè)內(nèi)容寫的有點(diǎn)亂,但是意思大概就是這個(gè)意思。主要想說(shuō)maven可以搞這樣一個(gè)事情。也給自己留個(gè)備忘錄。如果有人來(lái)看到這個(gè)希望輕噴,我很少寫東西。最近準(zhǔn)備練習(xí)寫東西,待改進(jìn)的地方還是很多的,所以見諒了。
到此這篇關(guān)于Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐的文章就介紹到這了,更多相關(guān)Maven profile配置管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- maven profile自動(dòng)切換環(huán)境參數(shù)的2種方法詳解
- 使用maven profile指定配置文件打包適用多環(huán)境的方法
- Maven管理SpringBoot Profile詳解
- 詳解Maven profile配置管理及激活profile的幾種方式
- maven的pom.xml中profiles的作用詳解
- maven多profile 打包下 -P參和-D參數(shù)的實(shí)現(xiàn)
- Maven 多profile及指定編譯問題的解決
- 詳解maven中profiles使用實(shí)現(xiàn)
- maven profile實(shí)現(xiàn)多環(huán)境配置的示例
- maven profile動(dòng)態(tài)選擇配置文件詳解
- maven中profile的使用
相關(guān)文章
SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼
這篇文章主要介紹了SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04詳解如何在SpringBoot項(xiàng)目中使用全局異常處理
在完整的項(xiàng)目開發(fā)中,異常的出現(xiàn)幾乎是無(wú)法避免的;如果凡是有可能出現(xiàn)異常的地方,我們都手動(dòng)的使用try-catch將其捕獲的話,會(huì)使得代碼顯得十分臃腫并且后期不好維護(hù)。本文介紹了pringBoot項(xiàng)目中使用全局異常處理的方法,需要的可以參考一下2022-10-10@RequestParam使用defaultValue屬性設(shè)置默認(rèn)值的操作
這篇文章主要介紹了@RequestParam使用defaultValue屬性設(shè)置默認(rèn)值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02Java實(shí)用工具之使用oshi獲取主機(jī)信息的方法
這篇文章主要介紹了Java實(shí)用工具之使用oshi獲取主機(jī)信息的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02springmvc+kindeditor文件上傳實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了springmvc+kindeditor文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Java項(xiàng)目中如何訪問WEB-INF下jsp頁(yè)面
這篇文章主要介紹了Java項(xiàng)目中如何訪問WEB-INF下jsp頁(yè)面,文章通過示例代碼和圖文解析介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08集合框架(Collections Framework)詳解及代碼示例
這篇文章主要介紹了集合框架(Collections Framework)詳解及代碼示例,文章涉及集合數(shù)組的區(qū)別,collection接口,iterator迭代器,list接口及其用法,LinkedHashSet集合等有關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11mybatis教程之動(dòng)態(tài)sql語(yǔ)句_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了mybatis教程之動(dòng)態(tài)sql語(yǔ)句,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-09-09