spring boot 如何指定profile啟動(dòng)
spring boot項(xiàng)目可為不同的環(huán)境配置相應(yī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>
本地開發(fā)使用開發(fā)環(huán)境,idea啟動(dòng)開發(fā)環(huán)境配置如下:
1、點(diǎn)擊Edit Configrations
2、配置如下圖
3、啟動(dòng)工程
控制臺(tái)打印了application-dev.yml中配置的變量
開發(fā)時(shí),也有需要一個(gè)工程啟動(dòng)多個(gè)實(shí)例的場景,idea支持一個(gè)spring boot項(xiàng)目啟動(dòng)多個(gè)實(shí)例。
方法非常簡單,只需要只需要按照上面的教程在idea再新建一個(gè)啟動(dòng)配置,把Active profiles指定為prod即可,如下圖:
通過下圖可以看到,本地可以啟動(dòng)多個(gè)spring boot 實(shí)例
多環(huán)境打包
1、運(yùn)行maven打包命令:
打包test:
mvn clean package -D maven.test.skip=true -P test
這樣打出來的包中yml文件只會(huì)包含:application.yml、application-test.yml
打包prod:
mvn clean package -D maven.test.skip=true -P test
這樣打出來的包中yml文件只會(huì)包含:application.yml、application-prod.yml
2、找到j(luò)ar包運(yùn)行
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啟動(dòng)工程時(shí)無法使用dev的配置,因?yàn)閠arget中沒有application-dev.yml。
需要將target刪除后,重新啟動(dòng)工程,這時(shí)候target中就會(huì)有全部的配置文件,就能使用dev的配置了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Mybatis無法掃描xml文件的解決
這篇文章主要介紹了SpringBoot整合Mybatis無法掃描xml文件的解決操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12mybatis-plus enum實(shí)現(xiàn)枚舉類型自動(dòng)轉(zhuǎn)換
本文主要介紹了mybatis-plus enum實(shí)現(xiàn)枚舉類型自動(dòng)轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07關(guān)于Java數(shù)組查詢的相關(guān)問題及實(shí)例
這篇文章主要介紹了關(guān)于Java數(shù)組查詢的相關(guān)問題及實(shí)例,需要的朋友可以參考下。2017-08-08java實(shí)現(xiàn)爬蟲爬網(wǎng)站圖片的實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)爬蟲爬網(wǎng)站圖片的實(shí)例代碼,需要的朋友可以參考下2018-06-06Spring Cloud Feign報(bào)錯(cuò)問題解決
這篇文章主要介紹了Spring Cloud Feign報(bào)錯(cuò)問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Springboot引入攔截器并放行swagger代碼實(shí)例
這篇文章主要介紹了Springboot引入攔截器并放行swagger代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11