詳解maven中profiles使用實(shí)現(xiàn)
使用的場(chǎng)景
常常遇到一些項(xiàng)目中多環(huán)境切換的問(wèn)題。比如在開(kāi)發(fā)過(guò)程中用到開(kāi)發(fā)環(huán)境,在測(cè)試中使用測(cè)試環(huán)境,在生產(chǎn)中用生產(chǎn)環(huán)境的情況。springboot中提供了 spring.profile.active的方式來(lái)實(shí)現(xiàn)多環(huán)境的切換,通過(guò)設(shè)置環(huán)境變量和啟動(dòng)參數(shù)的方式。但是這樣做終究不能一勞永逸,要么需要修改yml文件,要么需要記得啟動(dòng)的時(shí)候帶上參數(shù)。而利用maven的profiles,可以減少很多工作。讓我們通過(guò)幾個(gè)例子一步步的掌握使用maven的profiles屬性。
快速上手
pom.xml文件設(shè)置
<profiles>
<profile>
<!--不同環(huán)境Profile的唯一id-->
<id>dev</id>
<properties>
<!--profiles.active是自定義的字段(名字隨便起),自定義字段可以有多個(gè)-->
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>目錄結(jié)構(gòu)

application.yml
spring:
profiles:
active: @profiles.active@application-dev.yml中代碼如下
server: port: 7091
其他幾個(gè)文件我只是把端口號(hào)進(jìn)行了修改,方便打包看不同的效果。
maven打包與激活profiles
你可以執(zhí)行命令
mvn clean package -Ptest
然后啟動(dòng)jar包,可以看到j(luò)ar包啟動(dòng)的是test的配置,如果換成-Pdev啟動(dòng)的就是dev包的端口。
默認(rèn)啟動(dòng)方式
如果不帶-Ptest,啟動(dòng)的是 prod的端口。因?yàn)樵趐rofiles中我們看到有配置默認(rèn)的選項(xiàng)。
<activation> <activeByDefault>true</activeByDefault> </activation>
settings.xml中使用activeProfiles指定
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles> 通過(guò)IDEA的可視化的方式
當(dāng)然如果使用IDEA工具進(jìn)行開(kāi)發(fā),還可以使用可視化的方式進(jìn)行打包。

更高級(jí)的玩法
通過(guò)和pom結(jié)合的方式設(shè)置動(dòng)態(tài)參數(shù)
如果我們希望通過(guò)docker-maven-plugin插件,把編譯好的jar打包成docker并且傳入相應(yīng)的開(kāi)發(fā)、測(cè)試、生產(chǎn)的服務(wù)器中去。這個(gè)時(shí)候,我們就需要根據(jù)不同的條件去傳入不同的服務(wù)器。
在profiles中我們可以做以下定義
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.id>dev</profile.id>
<docker.host>http://dev.demo.com:2375</docker.host>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profile.id>test</profile.id>
<docker.host>http://test.demo.com375</docker.host>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.id>prod</profile.id>
<docker.host>http://prod.demo.com:2375</docker.host>
</properties>
</profile>
</profiles>而在build控件中我們可以使用以下配置
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>demo/${project.artifactId}</imageName>
<imageTags>
<imageTag>${project.version}-${current.time}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<forceTags>true</forceTags>
<dockerHost>${docker.host}</dockerHost>
<forceTags>true</forceTags>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>其中 ${project.artifactId} 和${project.version}是關(guān)于節(jié)點(diǎn)下面和的引用。${current.time}是在build-helper-maven-plugin定義的,我們回頭再研究。
${docker.host}則是我們?cè)趐rofiles中定義的,可以隨著我們選擇不同的profile,把jar包build成不同的docker鏡像,并傳入指定服務(wù)器。
通過(guò)和yml結(jié)合設(shè)置動(dòng)態(tài)參數(shù)
除了可以在pom中設(shè)置動(dòng)態(tài)參數(shù),使得其根據(jù)profile的不同選擇不同的參數(shù)。還可以通過(guò)設(shè)置不同的profile,讓yml選擇不同的參數(shù)。這點(diǎn)和快速上手的例子有點(diǎn)相似。具體如下:
設(shè)置profiles
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.id>dev</profile.id>
<eureka.url>http://127.0.0.1:8001/eureka</eureka.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profile.id>test</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.id>prod</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
<profile>
<id>new</id>
<properties>
<profile.id>new</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
</profiles>我們?cè)趐rofile中設(shè)置了一個(gè)eureka.url的屬性,就可以在yml中直接調(diào)用。
eureka:
client:
service-url:
defaultZone: @eureka.url@
registry-fetch-interval-seconds: 10
instance:
prefer-ip-address: true在IDEA調(diào)試和啟動(dòng)的時(shí)候,一般會(huì)報(bào)錯(cuò)如下:
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token.
解決方法就是引入yaml.sankeyaml的jar包
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>打包不同的資源
在profile打包yml文件的時(shí)候,如果我們解壓了jar包,會(huì)發(fā)現(xiàn)還是把所有的application-profile.yml文件給打包進(jìn)去了。這個(gè)可以通過(guò)設(shè)置打包參數(shù),只打包需要的application文件。
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prd</id>
<properties>
<env>prd</env>
</properties>
</profile>
</profiles>
<build>
<finalName>springmvc</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>dev/*</exclude>
<exclude>prd/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>目錄結(jié)構(gòu)如下:

到此這篇關(guān)于詳解maven中profiles使用實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)maven中profiles使用 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringData關(guān)鍵字查詢(xún)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了SpringData關(guān)鍵字查詢(xún)實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java中常見(jiàn)的查找算法與排序算法總結(jié)
數(shù)據(jù)結(jié)構(gòu)是數(shù)據(jù)存儲(chǔ)的方式,算法是數(shù)據(jù)計(jì)算的方式。所以在開(kāi)發(fā)中,算法和數(shù)據(jù)結(jié)構(gòu)息息相關(guān)。本文為大家整理了Java中常見(jiàn)的查找與排序算法的實(shí)現(xiàn),需要的可以參考一下2023-03-03
Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來(lái)實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
基于Java Gradle復(fù)制項(xiàng)目模塊過(guò)程圖解
這篇文章主要介紹了基于Java Gradle復(fù)制項(xiàng)目模塊過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
解決后端傳long類(lèi)型數(shù)據(jù)到前端精度丟失問(wèn)題
這篇文章主要介紹了解決后端傳long類(lèi)型數(shù)據(jù)到前端精度丟失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Spring @Primary和@Qualifier注解原理解析
這篇文章主要介紹了Spring @Primary和@Qualifier注解原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
java system類(lèi)使用方法示例 獲取系統(tǒng)信息
這篇文章主要介紹了java system類(lèi)使用方法,該類(lèi)中的方法都是靜態(tài)的。不能被實(shí)例化,沒(méi)有對(duì)外提供構(gòu)造函數(shù),該類(lèi)可以獲取系統(tǒng)信息2014-01-01
Java?Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時(shí)時(shí)間配置
Hutool是一個(gè)Java基礎(chǔ)工具類(lèi),對(duì)文件、流、加密解密、轉(zhuǎn)碼、正則、線程、XML等JDK方法進(jìn)行封裝,組成各種Util工具類(lèi),這篇文章主要給大家介紹了關(guān)于Java?Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時(shí)時(shí)間配置的相關(guān)資料,需要的朋友可以參考下2024-01-01

