1. 在pom.xml添加
<profiles> <profile> <id>dev</id> <properties> <environment>dev</environment> </properties> </profile> <profile> <id>prod</id> <properties> <environment>prod</environment> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory>
<!--打包时,指不要这些文件--> <excludes> <exclude>application-dev.properties</exclude> <exclude>application-prod.properties</exclude> </excludes> </resource> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>application-${profileActive}.properties</include> </includes> </resource> </resources> </build>
2. 在 application.properties 增加 spring.profiles.active=@profileActive@
3. 使用 mvn clean package -P dev 打包,这样打包出的文件就不包含 application-prod.properties
注: 如果用 idea IDE , 运行的时候这里要选一下: