前言
目前,企業(yè)項目的開發(fā)過程中,往往會使用配置文件來做一些配置項來實現(xiàn)項目部署的靈活性,避免硬編碼的方式在環(huán)境變化時需要對代碼進行重新編譯。但是往往在項目周期中存在著各種環(huán)境:如開發(fā)環(huán)境、測試環(huán)境以及生產(chǎn)環(huán)境等,而且在不同的運行環(huán)境中可能牽扯到大量的配置項變化。如果在不同的部署環(huán)境中切換,對配置文件的管理往往容易讓程序員感覺非常的混亂。
為了避免這種換亂,研發(fā)過程中也有比較多的手段進行。比如,有公司就采用VPN的虛擬網(wǎng)絡(luò)環(huán)境,讓測試環(huán)境、生產(chǎn)環(huán)境的網(wǎng)絡(luò)一致,讓程序員在不同環(huán)境中對版本進行發(fā)布時只需要對VPN進行切換即可。以免發(fā)生網(wǎng)絡(luò)配置項改錯,漏改等現(xiàn)象的發(fā)生。這樣個人覺得還不錯,唯一有一點句是調(diào)整網(wǎng)絡(luò)環(huán)境、設(shè)備環(huán)境的成本應(yīng)該也比較高。
當(dāng)然profile的方式應(yīng)該算是比較經(jīng)濟的。我知道的比如spring-boot、maven都可以支持到profile的方式來對不同環(huán)境進行指定。本文希望介紹一下,我理解的使用maven的profile方式來進行不同環(huán)境切換。講得不到位的地方希望看官嘴下留情,也多指定。
Maven 的 profile:
在maven的 pom.xml
文件中有一個配置項叫著profiles
節(jié)點,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<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é)點下可以填寫多個profile
其中profile
主要包含了三個屬性id
、properties
、activation
。
id
應(yīng)該是為了區(qū)分profile
用的。
properties
就是對應(yīng)的屬性。
activation
應(yīng)該是主要用來指定是否被默認(rèn)激活的,它還有一個子節(jié)點activeByDefault
, 如果子節(jié)點activeByDefault
內(nèi)的值為true表示他會被激活。它還有一些子節(jié)點,但是不知道什么用。后續(xù)看看在學(xué)習(xí)下。
實踐前期準(zhǔn)備
我準(zhǔn)備建立一個簡單的maven功能來實踐一下maven的profile
實現(xiàn)不同的配置管理,所以首先需要建議一個maven工程。本文采用的是idea進行試驗的。一下是我建立工程的結(jié)構(gòu)圖。
由于我只是需要對配置文件進行管理,所以是完全不需要建立任何java類就可以的。
-
首先建立了三個
profile
相關(guān)的配置文件develop.properties
、product.properties
、test.properties
-
在三個文件中我就只寫了一個屬性
app.name
,三個文件中分別為develop.maven.profile
、product.maven.profile
、test.maven.profile
. -
為了驗證在各類配置文件中都是可行的, 我建立了兩個供測試的配置文件
application.properties
、application.xml
.
application.properties 的內(nèi)容為:
application.xml的內(nèi)容為:
實踐一:
實踐一主要采用profile
+ filter
的方式實現(xiàn)內(nèi)容的注入。
該方式的思想是通過 filter下的文件編寫可變動的配置項,由filters
標(biāo)簽引入不同的配置文件項,然后提取不同的配置文件中的配置項填充到resources
下的配置文件中。
所以其中的關(guān)鍵標(biāo)簽包含了 profile
filter
resource
Maven的配置文件pom.xml
的內(nèi)容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?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 否則無法對內(nèi)容進行注入--> <filtering> true </filtering> <directory>src/main/resources</directory> <includes> <include>** /*.properties</include> <include>**/ *.xml</include> </includes> </resource> </resources> </build> </project> |
執(zhí)行maven命令進行打包:
1
|
mvn clean install |
執(zhí)行后打包結(jié)果targets
文件夾下的配置文件application.properties
、 application.xml
的占位置被填充。
當(dāng)然如果切換profiles
下的激活項,填充的內(nèi)容自然也會發(fā)生變化。或者采用maven命令進行激活項的切換:
1
|
mvn clean package -Ptest # 指定激活項的ID |
實踐二:
實踐二主要采用profile
直接將屬性配置到了profile
下的properties
節(jié)點下。此方法就不在需要多余的filter
properties文件配置了。
例如:
1
2
3
4
5
6
7
8
9
10
|
<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
中多了一個jdbc.url
屬性。那我們的application.properties
文件中同樣適用兩個占位符
1
2
|
app.name=${app.name} jdbc.url=${jdbc.url} |
同樣執(zhí)行maven
命令進行打包:
1
|
mvn clean install -Pproduct |
打包之后的 application.properties
內(nèi)容對應(yīng)變?yōu)?/p>
結(jié)束語
整個內(nèi)容寫的有點亂,但是意思大概就是這個意思。主要想說maven可以搞這樣一個事情。也給自己留個備忘錄。如果有人來看到這個希望輕噴,我很少寫東西。最近準(zhǔn)備練習(xí)寫東西,待改進的地方還是很多的,所以見諒了。
到此這篇關(guān)于Maven profile實現(xiàn)不同環(huán)境的配置管理實踐的文章就介紹到這了,更多相關(guān)Maven profile配置管理內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/maoye/article/details/108674145