springboot maven 多環境配置


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM