spring boot 如何指定profile啟動(dòng)
如下圖所示:
pom.xml配置如下:
<dependencies> 其他依賴 <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version><scope>runtime</scope> </dependency> <!--阿里的druid連接池--> <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version> </dependency></dependencies> <!--配置環(huán)境的profile--> <profiles><profile> <id>dev</id> <properties><!--使用${environment}獲取值--><environment>dev</environment> </properties></profile><profile> <id>test</id> <properties><environment>test</environment> </properties></profile><profile> <id>prod</id> <properties><environment>prod</environment> </properties></profile> </profiles> <build><finalName>spring-boot-lean-${environment}</finalName> <resources> <!--排除環(huán)境配置文件--> <resource><directory>src/main/resources</directory><excludes> <exclude>application-*.yml</exclude></excludes> </resource> <resource><directory>src/main/resources</directory><filtering>true</filtering><!-- 打包時(shí)包含的文件 --><includes> <include>application-${environment}.yml</include></includes> </resource></resources> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build>本地開(kāi)發(fā)使用開(kāi)發(fā)環(huán)境,idea啟動(dòng)開(kāi)發(fā)環(huán)境配置如下:1、點(diǎn)擊Edit Configrations
控制臺(tái)打印了application-dev.yml中配置的變量
開(kāi)發(fā)時(shí),也有需要一個(gè)工程啟動(dòng)多個(gè)實(shí)例的場(chǎng)景,idea支持一個(gè)spring boot項(xiàng)目啟動(dòng)多個(gè)實(shí)例。
方法非常簡(jiǎn)單,只需要只需要按照上面的教程在idea再新建一個(gè)啟動(dòng)配置,把Active profiles指定為prod即可,如下圖:
通過(guò)下圖可以看到,本地可以啟動(dòng)多個(gè)spring boot 實(shí)例
打包test:
mvn clean package -D maven.test.skip=true -P test
這樣打出來(lái)的包中yml文件只會(huì)包含:application.yml、application-test.yml
打包prod:
mvn clean package -D maven.test.skip=true -P test
這樣打出來(lái)的包中yml文件只會(huì)包含:application.yml、application-prod.yml
java -jar 名稱.jar --spring.profiles.active=prod
若打出來(lái)的是測(cè)試環(huán)境的包則運(yùn)行:
java -jar 名稱.jar --spring.profiles.active=test
補(bǔ)充一點(diǎn)
執(zhí)行 mvn clean package -D maven.test.skip=true -P test ,target目錄中只有application.yml、application-test.yml,此時(shí)使用idea啟動(dòng)工程時(shí)無(wú)法使用dev的配置,因?yàn)閠arget中沒(méi)有application-dev.yml。
需要將target刪除后,重新啟動(dòng)工程,這時(shí)候target中就會(huì)有全部的配置文件,就能使用dev的配置了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題3. Python基于requests實(shí)現(xiàn)模擬上傳文件4. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車(chē)5. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)特效6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. Python 中如何使用 virtualenv 管理虛擬環(huán)境8. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)9. Python sorted排序方法如何實(shí)現(xiàn)10. Python獲取B站粉絲數(shù)的示例代碼
