1.使用Intellij IDEA創建Spring Boot和Maven項目
2.Spring Boot項目下application.yaml(yaml支持中文)或者application.properties(properties不支持中文)
application.yaml
spring:
profiles:
active: @profileActive@
application.properties
spring.profiles.active=@profileActive@
3.創建不同環境下的配置文件
application-dev.yml、application-test.yml、application-prod.yml或者application-dev.properties、application-test.properties、application-prod.properties
4.pom.xml文件中配置profiles節點
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
5.使用maven命令打包成相應環境的程序包
生產環境
mvn clean package -Pprod -U
# 或者
mvn clean package -DprofileActive=prod -U
測試環境
mvn clean package -Ptest -U
# 或者
mvn clean package -DprofileActive=test -U
開發環境
mvn clean package -Pdev -U
# 或者
mvn clean package -DprofileActive=dev -U