配合maven pom配置
<profiles>
<profile>
<id>local</id>
<properties>
<profile.active>local</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>
配置文件使用pom中的配置

spring:
profiles:
active: ${profile.active}
打包时指定使用环境参数以打test包环境为例:
mvn clean package -P test -DskipTests
Profile注解指定bean是否加载到spring容器中例如:@Profile({"dev","test","prod"}) 只在"dev","test","prod"上生效,其中的值与spring.profiles.active变量对比

本文详细介绍如何使用Maven的profiles特性进行多环境配置管理,包括本地、开发、测试及生产环境的设置。通过示例展示如何在POM文件中定义不同环境的属性,并在打包时指定环境参数,如使用mvn clean package -Ptest -DskipTests来打test包。同时,介绍了如何利用Spring的Profile注解控制bean在不同环境下的加载。
168万+

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



