maven中配置profile节点:
第一步:配置profiles:
<project>
....
<profiles>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>
</project>
第二部:配置resource
<!-- 定义了变量配置文件的地址 -->
<filters>
<filter>src/main/resources/application-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>application-development.properties</exclude>
<exclude>application-production.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application.properties</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>application-${env}.properties</include>
<include>application.properties</include>
</includes>
</resource>
</resources>
第三步:application.properties文件中引用maven profile节点的值:
profiles.active=@env@
第四步:配置application-development.properties、application-test.properties
第五步:打包---------> mvn package -Ptest
打包结果:
application.properties文件内容为测试环境属性,只加载了测试环境配置文件