分環境打包核心點:spring.profiles.active
pom.xml中添加:
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <logback.loglevel>DEBUG</logback.loglevel> <spring.profiles.active>dev</spring.profiles.active> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>test</id> <properties> <logback.loglevel>INFO</logback.loglevel> <spring.profiles.active>test</spring.profiles.active> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <logback.loglevel>INFO</logback.loglevel> <spring.profiles.active>prod</spring.profiles.active> <profileActive>prod</profileActive> </properties> </profile> </profiles>
resources目錄下的配置文件:
其中,向application.yml文件中添加:
spring: profiles: active: @profileActive@
完成后,看看pom.xml文件中是有build模塊(一般創建springboot項目會在pom.xml文件下自動生成),如果沒有添加:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
然后再Terminal控制台輸入maven打包命令:
- 選擇dev環境(默認):
mvn clean package
- 選擇test環境:
mvn clean package -P test
- 選擇prod環境:
mvn clean package -P prod