1、pom.xml文件加入配置信息
profiles标签与build标签同级
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<!-- 默认环境-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
2、resources中创建配置文件
application.properties
application-dev.properties
application-prod.properties
application-test.properties
3、application.properties内容
spring.profiles.active=@profile.active@
4、如果出现mvn打包异常:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources
解决方法为修改maven-resources-plugin的版本
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<!--先排除所有配置文件-->
<exclude>application*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<!--只打包指定环境的配置文件-->
<include>application.properties</include>
<include>application-${profile.active}.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!--maven-resources-plugin异常在这里修改版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>