IDEA switches the environment configuration through maven

1. Environment and configuration

Insert picture description here
In this way, you can read the configuration by specifying parameters in application.properties

spring.profiles.active=dev

2. Read the configuration in the form of maven variables

2.1 First we add the pom file

    <build>
            <finalName>platform-provider</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>

                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>


            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>application.properties</include>
                    </includes>
                    <!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/**</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>

            <!-- 定义 filter,即该资源中的值将会用来替换同名属性(设置 filtering 为 true 的资源中的属性)-->
            <filters>
                <filter>
                    src/main/resources/application-${env}.properties
                </filter>
            </filters>


        </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <!--            这里是设置设置默认是哪个环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

2.2 Change in application.properties

Set the environment to variable assignment

spring.profiles.active=@env@

3.maven start

Start command

mvn clean package -P 环境名  -D maven.test.skip=true

Insert picture description here
A screenshot of the successful build, and then it can be deployed
Insert picture description here

Guess you like

Origin blog.csdn.net/hgdzw/article/details/110086481