解決的問題
springboot(當然別的也可以)多環境切換需要該配置文件,打包時不夠方便。
解決:
配置文件能讀取pom文件中的配置,根據命令選擇不同配置注入springboot的配置文件中
pom配置文件:
<!-- 環境 -->
<profiles>
<!-- 開發 -->
<profile>
<id>dev</id>
<activation>
<!--默認激活配置,maven打包默認選用的配置-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--當前環境自定義配置,標簽名自定義-->
<profile.name>dev</profile.name>
<profile.baseUrl>http://localhost:5501</profile.baseUrl>
</properties>
</profile>
<!-- 測試 -->
<profile>
<id>test</id>
<properties>
<!--當前環境-->
<profile.name>test</profile.name>
<profile.baseUrl>http://test.xxxx.com/xxx</profile.baseUrl>
</properties>
</profile>
<!-- 生產 -->
<profile>
<id>prd</id>
<properties>
<!--當前環境,生產環境為空-->
<profile.name>prd</profile.name>
<profile.baseUrl>http://api.xxx.cn/xxx</profile.baseUrl>
</properties>
</profile>
</profiles>
修改后的寫法
當然,別的配置項也能這樣寫,這樣寫的好處是只需修改一處別的不用修改
maven打包命令
mvn clean package -Ptest 這樣就能打成測試環境的包了
可能會出現的一些問題
- 占位符也可以這樣寫 @配置的key@, 不建議這樣使用
- 報@@ 解析錯誤, 再次執行打包命令: mvn clean package -Ptest
- 修改了pom文件中的配置,再次啟動項目環境沒有變化,因為ide啟動的是你之前的包所以你要重新使用打包命令打個新包
**** 碼字不易如果對你有幫助請給個關注****
**** 愛技術愛生活 QQ群: 894109590****