詳解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)文章
java保證對(duì)象在內(nèi)存中唯一性的實(shí)現(xiàn)方法
這篇文章主要介紹了java如何保證對(duì)象在內(nèi)存中的唯一性,如果創(chuàng)建多個(gè)對(duì)象的話,可能會(huì)引發(fā)出各種各樣的問(wèn)題,這時(shí),就需要我們保證這個(gè)對(duì)象在內(nèi)存中的唯一性,需要的朋友可以參考下2019-06-06Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識(shí)
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(一)之MyBatis的基礎(chǔ)知識(shí),需要的朋友可以參考下2017-05-05Java集合框架之LinkedHashSet類(lèi)解讀
這篇文章主要介紹了Java集合框架之LinkedHashSet類(lèi)解讀,LinkedHashSet是HashSet的有序版本,它跨所有元素維護(hù)一個(gè)雙向鏈接的List,當(dāng)需要維護(hù)迭代順序時(shí),就使用這個(gè)類(lèi),當(dāng)遍歷HashSet時(shí),順序是不可預(yù)測(cè)的,需要的朋友可以參考下2023-09-09Spring 校驗(yàn)(validator,JSR-303)簡(jiǎn)單實(shí)現(xiàn)方式
這篇文章主要介紹了Spring 校驗(yàn)(validator,JSR-303)簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java事務(wù)管理學(xué)習(xí)之JDBC詳解
這篇文章主要介紹了Java事務(wù)管理學(xué)習(xí)之JDBC的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03idea2020安裝MybatisCodeHelper插件的圖文教程
這篇文章主要介紹了idea2020安裝MybatisCodeHelper插件的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09springboot實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07SpringBoot2.x 整合 AntiSamy防御XSS攻擊的簡(jiǎn)單總結(jié)
本文主要對(duì)SpringBoot2.x集成AntiSamy防御XSS攻擊進(jìn)行簡(jiǎn)單總結(jié),其中SpringBoot使用的2.4.5版本,通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08