springboot yml multi-environment configuration and profile packaging

1.yml multi-environment configuration

In Spring Bootthe multi-environment configuration file name needs to meet application-{profile}.ymlthe format, which {profile}corresponds to your environment identification;

application-dev 开发环境
application-test 测试环境
application-prod 生产环境

If we want to activate a certain environment, we only need to  application.yml:

spring:
  profiles:
    active: dev #可修改位test prod

2. Create a multi-environment profile package

Through the above steps, you can easily switch the current environment, but it is a little troublesome. Are there some configuration files that can replace manually changing the profile and create a multi-environment profile package?

Add a profile node to the pom file, and add packaged and filtered configuration file rules to the resources node under build

 <profiles>
        <profile>
            <!-- 开发环境  -->
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <!-- 默认激活的环境  -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境  -->
            <id>test</id>
            <properties>
                <profileActive>test</profileActive>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境  -->
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
        </profile>
    </profiles>
    
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${profileActive}.yml</include>
                    <include>application.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

Configure a dynamic property to take place in application.yml, the default separator is @property name@, this property will be replaced by the parameters passed in when maven is packaged;

spring:
  profiles:
    active: @profileActive@

The visual selection environment on the right makes the work more efficient; maven multi-environment packaging.

 You can choose prod text for packaging, 

 

 

Guess you like

Origin blog.csdn.net/deng_zhihao692817/article/details/131192559