使用maven的filter
<filters> <filter>src/main/filters/filter-${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <properties> <env>dev</env> </properties> <profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <profile> <id>product</id> <properties> <env>product</env> </properties> </profile> </profiles> 在测试和部署上线时分别通过-P传入当前的profile id,这样maven就会将env变量设置为对应的值,从而导致使用不同的filter文件来对resources下的文件进行过滤替换。
例如:当调用maven package时传入-Pdev参数,则会使用
filter-dev.properties中的内容来替换resources目录中的配置文件
使用Spring boot 规范
配置激活选项
在 application.properties中激活
spring.profiles.active=dev- 运行的时候还可以制定激活的环境
java -jar myapp.jar --spring.profiles.active=prd- 添加其他配置文件

本文介绍如何使用Maven的过滤功能实现不同环境下的资源文件配置切换,并结合Spring Boot的环境激活选项,使得开发者能够轻松地在开发、测试及生产环境中进行配置切换。

被折叠的 条评论
为什么被折叠?



