[Java][Spring]spring profile与maven profile多环境管理
spring profile 与 maven profile 多环境管理
spring profile
Spring profile是Spring提供的多环境管理方案。
如下图:

每种环境都对应一个yml文件,然后再application.yml中配置需要使用的环境:
spring:
#环境 dev|test|pro|preview
profiles:
active: dev
可以看出,Spring profile对配置文件的命名是有有要求的,必须是 application- 开头。
除了配置环境之外,一些不随着环境变化而变化的配置也应该放到application.yml中,application-xxx.yml的配置文件最好值存放于环境相关的配置项。
通过改变spring.profiles.active的值来切换不同的环境,这种方法简单易懂,但是其实还有两个问题:
- 每次切换环境都要手动修改
spring.profiles.active的值 - 打包的时候需要手动删除其他的环境的配置文件,不然其他环境的敏感信息会被一起打包
为了解决这两个问题,我们需要 maven profile 的配合。
maven profile
maven的profile可以让我们定义多套配置信息,并指定其激活条件,然后在不同的环境下使用不同的profile配置。
在maven中有两个地方可以配置profile:
pom.xml中:这里面定义的profile作用范围是当前项目。(user)/.m2/setting.xml中:这里面定义的profile作用范围是所有使用了该配置文件的项目。
pom.xml中的profile
在pom.xml中,profile能定义的东西就很多了
<profiles>
<profile>
<id>..</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
</profiles>
我们从spring profile遗留下来的两个问题来进行分析配置:
-
每次切换环境都要手动修改
spring.profiles.active的值这个问题通过配置
profile就可以解决,在pom根节点中添加:<profiles> <profile> <id>dev</id> <activation> <!-- activeByDefault为true表示,激活默认id的profile(这里是dev)--> <activeByDefault>true</activeByDefault> </activation> <!-- properties里面可以添加自定义节点,如下添加了一个env节点 --> <properties> <!-- 这个节点的值可以在maven的其他地方引用,即定义了一个叫env的变量 --> <env>dev</env> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles>如上可以看到,定义了三套环境,其中
id为dev的是默认环境,三套环境都定义了叫env的变量。如果用的是
idea编译器,添加完成之后,maven空间窗口会多出一个profiles,其中的默认值就是设置的dev。
这样的话,最小化的
profiles已经配置好了,通过勾选上图的profiles,就可以快速切换maven的profile环境。现在可以根据勾选来配置
maven profile的环境,但是spring profile还得通过手动修改spring.profiles.active的值来切换,这个问题,可以这么做:还记得
maven profile中定义的env变量吗,现在就可以进行使用了:spring: profiles: # 将maven profile和spring profile环境关联起来 active: @env@当
maven profile将环境切换为test的时候,在pom中定义的id为test的profile环境被激活,在该环境下的env的值是test,maven插件会将@env@替换为test,这样spring profile的环境也随之发生了改变。可以看到,自定的变量
env的值不能乱写,要与spring profile的环境相对应。
总结一下:
- 先在
pom文件中配置profiles - 然后在
application.yml配置文件中添加spring.profiles.active=@env@
- 先在
-
打包的时候需要手动删除其他的环境的配置文件,不然其他环境的敏感信息会被一起打包
解决这个问题,需要在
pom的根节点中配置build的信息。<build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <!--先排除application开头的配置文件--> <exclude>application*.yml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <!--filtering 需要设置为 true,这样在include的时候,才会把 配置文件中的@env@ 这个maven`变量`替换成当前环境的对应值 --> <filtering>true</filtering> <includes> <!--引入所需环境的配置文件--> <include>application.yml</include> <include>application-${env}.yml</include> </includes> </resource> </resources> </build>directory:资源文件所在目录includes:需要包含的文件列表excludes:需要排除的文件列表
如上,配置了两个
<resouce>,第一个resouce排除了src/main/resources目录下所有以application开头的配置文件;第二个resouce在第一个的基础上添加了所需的配置文件。注意
application-${env}.yml它是一个动态变化的值,随着当前环境的改变而改变。比如当前环境的id是dev的profile,那么env的值就是dev。这样配置之后,
maven在build的时候,就会根据配置先排除掉指定的配置文件,然后根据当前环境添加所需要的配置文件。
settings.xml中的profile
由于settings.xml的作用范围广,所以profile中只能定义一些公共的信息,如下:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<profiles>
<profile>
<id>...</id>
<activation>...</activation>
<repositories>...</repositories>
</profile>
</profiles>
...
</settings>
id:该profile的唯一标识activation:在哪些情况下激活profile,这里有很多中策略可以选择,只要满足其中一个条件就能激活repositories:远程仓库
可以看到,其实能配置的东西有限,所以一般都会配置将maven profile配置在pom.xml中。
本文来自博客园,作者:knqiufan,转载请注明原文链接:https://www.cnblogs.com/knqiufan/p/16367224.html

浙公网安备 33010602011771号