http://xiangai.taobao.com
http://shop148612228.taobao.com

使用yml多环境配置和创建多环境profile打包 springboot俩个@@ active: @profileActive@

1、yml多环境配置
在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识;

application-dev 开发环境
application-test 测试环境
application-prod 生产环境
1
2
3
如果我们要激活某一个环境,只需要在 application.yml里:

spring:
profiles:
active: dev
1
2
3


此外,假设我们配置一些基本设置如:

application-dev.yml

server:
port: 9001
1
2
application-test.yml

server:
port: 9002
1
2
application-prod.yml

server:
port: 9003
1
2
此时,当我们去修改application.yml:

改为 dev


改为 test


改为 prod


2、创建多环境profile打包
通过上述步骤,可以轻松切换当前环境,不过也稍微有些麻烦,那是否有一些配置文件可以代替手动更改profile并且能创建多环境profile打包呢?

答案是肯定的
1
pom.xml

pom文件中添加profile节点,并在build下的resources节点添加打包过滤的配置文件规则

<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<!-- 默认激活的环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.yml</include>
<include>application.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
在application.yml中配置一个动态属性进行占位,默认的分隔符是@属性名@,这个属性会通过maven打包时传入参数进行替换;

spring:
profiles:
active: @profileActive@
1
2
3


右侧的可视化选择环境,让工作更加变得高效;

maven 多环境打包

打包过滤配置文件规则也是用一个占位符进行占位,打包时也会通过maven传入参数进行替换。

1、通过 -D命令传入属性值profileActive,如:
clean install -Dmaven.test.skip=true -DprofileActive=dev
1
2、通过-P命令指定profile环境,如:
clean package -P prod
1
右侧可视化选择更加方便:

 

🆗闲话不多说,好了,这一小章到此结束;

代码仓库地址:点击进入
————————————————
版权声明:本文为CSDN博主「解忧杂货铺Q」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_26003101/article/details/110877736

posted @   万事俱备就差个程序员  阅读(812)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2021-07-09 MySQL变量的使用
2021-07-09 java lambda求和最值
2020-07-09 MySQL中tinytext、text、mediumtext和longtext等各个类型详解
2020-07-09 [Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual.  You have to change some columns to
2020-07-09 关于@PropertySource注解对于yml的支持
2020-07-09 yml配置文件读取出错 Exception in thread "main" while scanning for the next token found character '\t(TAB)'
2019-07-09 异常来自 HRESULT:0x8007000B

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏
点击右上角即可分享
微信分享提示