项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置等资源配置了,试想有生产环境、预发环境、测试环境等,需要为不同的场景下来动态的改变资源配置值。而使用maven Profile就可以帮我们解决这些问题。
多环境是指,有多个profile环境,每个环境有自己不同的配置(有的定义在pom.xml中,有的定义在.properties文件中),总共分三步:
1、在pom.xml中定义多个profile
2、在resources里新建目录存放不同环境的配置文件
3、在pom.xml中定义加载的环境的配置,指定到使用的profile文件
第一步:
<profiles>
<profile>
<id>development</id>
<properties>
<profiles.active>development</profiles.active>
<!-- log -->
<log.path>D://export//Logs//com.test.local</log.path>
<log.maxHistory>24</log.maxHistory>
<log.totalSizeCap>2GB</log.totalSizeCap>
<log.ConversionPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] [%C{1},%M\(\),%L] [%X{Jst_TRACE_ID}]- %m%n
</log.ConversionPattern>
</properties>
</profile>
<profile>
<id>yfb</id>
<properties>
<profiles.active>yfb</profiles.active>
<!-- log -->
<log.path>/export/Logs/com.test.local</log.path>
<log.maxHistory>24</log.maxHistory>
<log.totalSizeCap>2GB</log.totalSizeCap>
<log.ConversionPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] [%C{1},%M\(\),%L] [%X{Jst_TRACE_ID}]- %m%n
</log.ConversionPattern>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<profiles.active>production</profiles.active>
<!-- log -->
<log.path>/export/Logs/com.test.local</log.path>
<log.maxHistory>24</log.maxHistory>
<log.totalSizeCap>2GB</log.totalSizeCap>
<log.ConversionPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] [%C{1},%M\(\),%L] [%X{Jst_TRACE_ID}]- %m%n
</log.ConversionPattern>
</properties>
</profile>
</profiles>
第二步:
第三步:
<build>
<finalName>pp-pharmacist-web</finalName>
<resources>
<!-- 在build中增加resources节点指定资源加载的目录,filtering为true自动替换${}的值 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>development/*</exclude>
<exclude>yfb/*</exclude>
<exclude>production/*</exclude>
</excludes>
</resource>
<!-- build中一旦增加了resources节点,默认的resource目录就失效了,需要重新指定资源目录 -->
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources>
</build>
参考文章:
https://blog.youkuaiyun.com/u011781521/article/details/77899467
https://blog.youkuaiyun.com/luckyzhoustar/article/details/50411962
https://www.cnblogs.com/dreamroute/p/6729147.html