SoFunction
Updated on 2025-04-27

Implementation of configuring multi-environment parameters when maven is packaged

1. pom configuration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    
    <!-- Other configurations -->

    <build>
        <plugins>
            <plugin>
                <groupId></groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- Only package configuration files for the specified environment -->
                <includes>
                    <include>application-${}.properties</include>
                    <include></include>
                </includes>
                <!-- Enable filtering That is, the variables in the resource will be replaced by the value in the filter -->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <>dev</>
            </properties>
            <!-- Default environment -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>long</id>
            <properties>
                <>long</>
            </properties>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <>sit</>
            </properties>
        </profile>
    </profiles>

</project>

2. Application configuration

spring:
  profiles:
    active: @@

Here is consistent with the properties attribute in the above configuration file

3. Packing

mvn clean package -Plong -=true

-Plong: Specify the packaged environment as long

4. Pay attention

Because in the pom file, if the packaging configuration is modified, it will only be packaged into the configuration file of the specified environment, so you no longer need to specify the environment parameters at startup.

This is the end of this article about the implementation of configuring multi-environment parameters when maven packaging. For more related content on configuring multi-environment parameters when maven packaging, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!