spring boot 如何指定profile啟動
如下圖所示:
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>本地開發(fā)使用開發(fā)環(huán)境,idea啟動開發(fā)環(huán)境配置如下:1、點(diǎn)擊Edit Configrations
控制臺打印了application-dev.yml中配置的變量
開發(fā)時(shí),也有需要一個(gè)工程啟動多個(gè)實(shí)例的場景,idea支持一個(gè)spring boot項(xiàng)目啟動多個(gè)實(shí)例。
方法非常簡單,只需要只需要按照上面的教程在idea再新建一個(gè)啟動配置,把Active profiles指定為prod即可,如下圖:
通過下圖可以看到,本地可以啟動多個(gè)spring boot 實(shí)例
打包test:
mvn clean package -D maven.test.skip=true -P test
這樣打出來的包中yml文件只會包含:application.yml、application-test.yml
打包prod:
mvn clean package -D maven.test.skip=true -P test
這樣打出來的包中yml文件只會包含:application.yml、application-prod.yml
java -jar 名稱.jar --spring.profiles.active=prod
若打出來的是測試環(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啟動工程時(shí)無法使用dev的配置,因?yàn)閠arget中沒有application-dev.yml。
需要將target刪除后,重新啟動工程,這時(shí)候target中就會有全部的配置文件,就能使用dev的配置了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. phpstudy apache開啟ssi使用詳解2. ASP.NET Core按用戶等級授權(quán)的方法3. ASP常用日期格式化函數(shù) FormatDate()4. HTML中的XML數(shù)據(jù)島記錄編輯與添加5. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法6. 詳解瀏覽器的緩存機(jī)制7. .NET 中配置從xml轉(zhuǎn)向json方法示例詳解8. ASP新手必備的基礎(chǔ)知識9. ASP中if語句、select 、while循環(huán)的使用方法10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
