分环境打包核心点:spring.profiles.active
pom.xml中添加:
1<profiles> 2 <profile> 3 <id>dev</id> 4 <activation> 5 <activeByDefault>true</activeByDefault> 6 </activation> 7 <properties> 8 <logback.loglevel>DEBUG</logback.loglevel> 9 <spring.profiles.active>dev</spring.profiles.active> 10 <profileActive>dev</profileActive> 11 </properties> 12 </profile> 13 <profile> 14 <id>test</id> 15 <properties> 16 <logback.loglevel>INFO</logback.loglevel> 17 <spring.profiles.active>test</spring.profiles.active> 18 <profileActive>test</profileActive> 19 </properties> 20 </profile> 21 <profile> 22 <id>prod</id> 23 <properties> 24 <logback.loglevel>INFO</logback.loglevel> 25 <spring.profiles.active>prod</spring.profiles.active> 26 <profileActive>prod</profileActive> 27 </properties> 28 </profile> 29</profiles>
resources目录下的配置文件:

其中,向application.yml文件中添加:
1spring: 2 profiles: 3 active: @profileActive@
完成后,看看pom.xml文件中是有build模块(一般创建springboot项目会在pom.xml文件下自动生成),如果没有添加:
1<build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 <executions> 7 <execution> 8 <goals> 9 <goal>repackage</goal> 10 </goals> 11 </execution> 12 </executions> 13 </plugin> 14 </plugins> 15</build>
然后再Terminal控制台输入maven打包命令:
-
选择dev环境(默认):
mvn clean package
-
选择test环境:
mvn clean package -P test
-
选择prod环境:
mvn clean package -P prod