1.在src/main/resources下建立filters文件夹,下面设置需要切换的多个配置文件
2.修改pom.xml
<profiles>
<profile>
<id>dev</id>
<build>
<filters>
<filter>
src/main/resources/filters/dev.properties
</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>dev</name>
</property>
</activation>
</profile>
<profile>
<id>staging</id>
<build>
<filters>
<filter>
src/main/resources/filters/staging.properties
</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<filters>
<filter>
src/main/resources/filters/prod.properties
</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>qa</id>
<build>
<filters>
<filter>
src/main/resources/filters/qa.properties
</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
3.打包时加载不同的配置
备注:在pom.xml中加载了如下的maven打包插件
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.path.to.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
4.可能遇到的问题
Error loading property file 'src/main/resources/filters/dev.properties' (org.apache.maven.plugins:maven-resources-plugin:2.6:resources:default-resources:process-resources)
解决方法:
${basedir}/src/main/resources/filters/dev.properties