基于springboot的maven项目多环境自由切换
由于springboot支持设置环境变量spring.profiles.active来切换环境,所以我们可以在maven中定义一个环境变量来动态改变环境。
1.在maven中将所有的环境添加到profiles节点中
2.在项目中配置多个环境配置如application-dev.properties,application-test.properties,再添加一个application.properties用于存放公共的配置
3.在application.properties中声明一个属性spring.profiles.active=@spring-env@,其中spring-env与maven中定义需要保持一致。
maven中配置如下:
<profiles>
<profile>
<id>local</id>
<properties>
<spring-env>local</spring-env>
</properties>
<!-- 是否默认 true表示默认-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>sit</id>
<properties>
<spring-env>sit</spring-env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring-env>prod</spring-env>
</properties>
</profile>
</profiles>
application.properites配置:
spring.profiles.active=@spring-env@
探索不以困难而止,学习不以艰苦而终

浙公网安备 33010602011771号