There are often multiple environments during development, such as development environment, test environment, production environment, and so on. It would be tedious and unintelligent to write configuration files in a single file and manually switch between different environments

Multi-environment configuration for Maven project

Three core points:

  1. Create a master configuration file with the required configuration items, but with the property values${xxx}In the form of writing
  2. Create value files for different environments with specific values that need to be dynamically loaded into the master profile
  3. Configure profiles in POM.xml

1. Master configuration file

For demonstration purposes, just create a configuration file named application.properties in the SRC /main/resources directory as follows:

application.name=${application.name}
Copy the code

2. Files with different environment values

Create a properties folder under SRC /main and create application-dev.properties and application-prod.properties under properties respectively as follows:

  1. application-dev.properties
application.name=application-dev
Copy the code
  1. application-prod.properties
application.name=application-prod
Copy the code

3. The pom file

<build>
    <filters>
        <! ${env} -- ${env} -->
        <! -- if activated dev environment, then the corresponding file is SRC/main/properties/application - dev. The properties - >
        <filter>src/main/properties/application-${env}.properties</filter>
    </filters>
    <resources>
        <resource>
            <! The directory where the real configuration file is located -->
            <directory>src/main/resources</directory>
            <! Set this parameter to true to enable dynamic substitution -->
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
<profiles>
    <! -- Environment I -->
    <profile>
        <! --> < P style = "max-width: 100%; clear: both; clear: both;
        <! -- id can be arbitrarily named, cannot be repeated, and can be the same as env.
        <id>develop</id>
        <properties>
            <! Application -${env}.properties -->
            <! ${env}. Properties = application-${env}.
            <env>dev</env>
        </properties>
        <! The dev environment can be placed in any environment, but not in more than one environment.
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <! -- Environment II -->
    <profile>
        <id>product</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>
Copy the code

demo

  1. MVN clean package -dmaven.test. skip=true

    application.name=application-dev
    Copy the code

    Note that no profile is specified here, but we set the default environment to dev, so the value of application-dev.properties is typed into application.properties

  2. MVN clean package-pproduct – dmaven.test. skip=true

    application.name=application-prod
    Copy the code

    Note that -p specifies the profile ID configured in pom. XML, not the value of the tag env

  3. If will build: resources: the resource: filtering label commented out, or set to false, the packaged application. After the properties content is as follows:

    application.name=${application.name}
    Copy the code

    So if you want to pass profile dynamic loading configuration files, the build: resources: the resource: filtering the label must be set to true

Spring Boot multi-environment configuration

Spring Boot multi-environment configuration is faster than normal Maven projects

1. Master configuration file

Create application.properties or application.yml under SRC /main/resources. Create application.yml as follows:

spring:
  profiles:
    active: @profileActive@
Copy the code

2. Files with different environment values

Create application-dev.yml in SRC /main/resources as follows:

server:
  port: 8088
Copy the code

Create application-prod.yml in SRC /main/resources as follows:

server:
  port: 8099
Copy the code

3. The pom file

<profiles>
    <profile>
        <! -- Profile ID, specified for packaging -->
        <id>dev</id>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
        <! -- Activate the dev environment by default -->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profileActive>prod</profileActive>
        </properties>
    </profile>
</profiles>
Copy the code

You can see that Spring Boot poM files work without the build tag, which is much friendlier than normal Maven projects