項目構建的時候,需要根據不同的場景來改變項目中的屬性資源,最爲常見的莫過於數據庫連接配置等資源配置了,試想有生產環境、預發環境、測試環境等,需要爲不同的場景下來動態的改變資源配置值。而使用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.csdn.net/u011781521/article/details/77899467
https://blog.csdn.net/luckyzhoustar/article/details/50411962
https://www.cnblogs.com/dreamroute/p/6729147.html