前言
我们的spring boot项目一般会有三种配置application-dev.properties、application-test.properties、application-prd.properties,有的公司会更多,这种做的目的是为了在切换不同环境的时候使用不同的配置文件,防止多个环境使用同一个配置文件改过去改过来出现问题。
像我们公司的环境配置有8种
内容
目前企业中很多都是在用jenkins打包项目到测试环境和正式环境,jenkins在打包的时候可以指定打包环境使用哪种配置。
在jenkins中指定一下运行时使用的环境
mvn clean package -U -Ptest 使用test配置
mvn clean package -U -Pdev 使用dev配置
mvn clean package -U -Pprd 使用prd配置
mvn clean package -U -Popen 使用open配置
相信大家能够看出来了-P后面就是指定环境配置id,那么这个id会作用在哪呢?
这个id会作用在项目中的pom.xml文件中,只要我们的pom.xml文件中有profiles,在maven打包的时候就会把id传到这里面,如果能够匹配上就会使用对应</env中配置的文件。当然这只是其中一种办法,还有很多其他办法,我这里就不介绍了。
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
<env>bootstrap-dev.properties</env>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<spring.profiles.active>test</spring.profiles.active>
<env>bootstrap-test.properties</env>
</properties>
</profile>
<profile>
<id>prd</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<spring.profiles.active>prd</spring.profiles.active>
<env>bootstrap-prd.properties</env>
</properties>
</profile>
</profiles>