使用maven profile指定配置文件打包适用多环境的方法
作者:拉轰的大蜗牛 时间:2022-06-30 03:49:11
开发过程, 我们习惯把数据源配置, 项目常量, 日志配置等基础数据配置写到一个个单独的的文件中. 如jdbc.properties等各种.格式的文件.
如何不频繁修改配置文件, 随时打包不同基础数据配置信息的项目.
1.新建maven项目, 在pom.xml中添加 profile节点信息如下:
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<environment>development</environment><!-- 节点名字environment是自己随意取的 -->
</properties>
<activation>
<activeByDefault>true</activeByDefault><!-- 默认激活该profile节点-->
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<!-- 预演环境 -->
<id>prev</id>
<properties>
<environment>preview</environment>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<environment>production</environment>
</properties>
</profile>
</profiles>
2. 在项目中添加各环境需要的数据源配置文件,分不同目录存放, 分别是开发,测试, 预演,生产 环境. 如下图左边部分所示.
3. pom.xml中配置resource节点信息.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>environment/development/*</exclude>
<exclude>environment/test/*</exclude>
<exclude>environment/preview/*</exclude>
<exclude>environment/production/**</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/environment/${environment}</directory>
<targetPath>environment/${environment}</targetPath>
</resource>
</resources>
</build>
resource节点信息解释:
<directory>src/main/resources</directory> <!--打包时包含src/main/resources目录下所有"子"文件 和 "孙"文件.如config 和environment -->
<exclude>environment/development/**</exclude> <!--打包时排除src/main/resources/environment/development下所有"子"文件 和 "孙"文件.-->
<exclude>environment/test/**</exclude> <!--打包时排除src/main/resources/environment/test下所有"子"文件 和 "孙"文件.-->
<exclude>environment/preview/**</exclude> <!--打包时排除src/main/resources/environment/preview下所有"子"文件 和 "孙"文件.-->
<exclude>environment/production/**</exclude><!--打包时排除src/main/resources/environment/production下所有"子"文件 和 "孙"文件.-->
<!-- 注意点: 如果写一个心号*, 如<exclude>environment/development/*</exclude> 则表示:打包时排除src/main/resources/environment/development下所有"子"文件, 不排除"孙"文件, 如上一个截图所示.-->
<!-- 以上配置优先度从上到下 递增, 这就达到目的: config目录下的配置各环境都需要, 而其它环境相关的配置只会有一个目录被打包--->
<directory>src/main/resources/environment/${environment}</directory> <!-- 打包时包含src/main/resources/environment/${environment}下所有"子"文件,environment变量值来自profile中赋值 -->
<targetPath>environment/${environment}</targetPath><!--指定src/main/resources/environment/${environment}所有"子文件" 打包 到包的哪个目录 -->
4. 项目打包. 打包命令mvn package -Pdev来指定激活id为 dev 的profile节点, 这样, 开发环境配置文件就会被打包.
开发: mvn package -Pdev (因为配置了默认激活dev部分, 所以也可以使用mvn package, 这与 mvn package -Pdev 效果相同)
测试: mvn package -Ptest
预演:mvn package -Pprev
生产:mvn package -Pprod
来源:https://blog.csdn.net/hjiacheng/article/details/57413933
标签:maven,profile,多环境,打包
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java.math.BigDecimal的用法及加减乘除计算
2022-01-15 15:55:20
IDEA 2019.2.3破解激活教程(亲测有效)
2023-02-21 22:16:23
![](https://img.aspxhome.com/file/2023/2/62952_0s.jpg)
Java中super和this关键字详解
2023-03-18 15:49:08
![](https://img.aspxhome.com/file/2023/4/63054_0s.png)
java使用ffmpeg处理视频的方法
2023-07-28 22:58:08
![](https://img.aspxhome.com/file/2023/9/57719_0s.jpg)
详解Java中Callable和Future的区别
2023-07-25 21:18:58
![](https://img.aspxhome.com/file/2023/8/63038_0s.jpg)
windows如何使用bat脚本后台启动/停止和重启jar包服务
2022-02-27 12:28:36
Java BIO,NIO,AIO总结
2022-02-07 01:48:28
![](https://img.aspxhome.com/file/2023/1/61551_0s.png)
JPA Specification常用查询+排序实例
2023-11-23 04:56:32
![](https://img.aspxhome.com/file/2023/3/59423_0s.png)
Java设计模式之命令模式
2022-06-17 22:49:07
关于重写equals()方法和hashCode()方法及其简单的应用
2023-08-01 06:48:13
![](https://img.aspxhome.com/file/2023/5/57805_0s.jpg)
Spring Cloud Config实现分布式配置中心
2022-04-25 16:28:07
解决微服务feign调用添加token的问题
2023-09-11 09:38:05
![](https://img.aspxhome.com/file/2023/1/58331_0s.jpg)
SpringBoot获取前台参数的六种方式以及统一响应
2023-08-22 21:25:23
![](https://img.aspxhome.com/file/2023/6/58336_0s.png)
java实现文件的断点续传
2023-11-23 09:11:00
![](https://img.aspxhome.com/file/2023/4/59544_0s.jpg)
快速理解Java垃圾回收和jvm中的stw
2021-09-06 20:27:17
使用IDEA搭建一个简单的SpringBoot项目超详细过程
2022-07-19 11:43:58
![](https://img.aspxhome.com/file/2023/3/64033_0s.png)
spring boot微服务场景下apollo加载过程解析
2022-05-20 13:55:32
java插入排序 Insert sort实例
2023-07-21 07:07:37
hibernate 命名查询如何实现
2023-11-04 02:35:41
MyBatis一对多嵌套查询的完整实例
2023-07-12 02:49:56