preface

There are at least three environments in daily development: Development environment (DEV), test environment (Test) and production environment (PROD).

Different environments have different configurations, such as databases, ports, IP addresses, and so on.

So how do you differentiate and package all these environments?

This article will introduce how to configure and package multiple environments in Spring Boot.

Spring Boot provides multi-environment configuration

Spring Boot already has good support for multi-environment integration, with the ability to switch environments freely between packaging and running.

So how do you configure it? The following will be introduced step by step.

Create profiles for different environments

Since the configuration of each environment is different, we simply put the configuration of the different environment in different configuration files, so we need to create three different configuration files. Properties, application-test.properties, application-prod.properties.

Note: The name of the configuration file must be in application-name.properties or application-name.yml format. This name can be customized, mainly used for differentiation.

At this point there are four configuration files in the entire project, plus application.properties.

Specify the environment to run in

Although you have created configuration files for each environment, Spring Boot still does not know which environment you want to run. You can specify this in two ways:

Specified in the configuration file

Properties or application.yml file, which reads as follows:

Specify run environment as test environment
spring.profiles.active=test
Copy the code

What does this configuration do?

If no environment is specified, Spring Boot loads the application.properties file by default, which in turn tells Spring Boot to find the configuration file for the Test environment.

Specified when you run the JAR

Spring Boot’s built-in environment switch allows you to specify the environment when running the Jar package, using the following command:

java -jar xxx.jar --spring.profiles.active=test
Copy the code

The above command specifies the environment to run test, is convenient?

Multi-environment configuration for Maven

Maven itself provides support for multiple environments, not just Spring Boot projects, but any Maven-based project that can be configured.

Maven’s support for multiple environments is even more powerful in terms of functionality, allowing for JDK versions, resource files, operating systems, and so on.

How do you configure it? Let’s take a look at each of them.

Create multiple environment profiles

Properties; application-test.properties; application-prod.properties;

Plus the default configuration file application.properties is also four profiles.

Define active variables

To apply the Maven-activated environment to Spring Boot, you still use the spring.profiles.active property, but now the value of this property will be set to Maven. The configuration is as follows:

spring.profiles.active=@profile.active@
Copy the code

Profile. active is actually a variable, and the -p test specified during Maven packaging is passed in as the value.

Profiles are defined in the POM file

You need to define profiles for different environments in Maven’s POM.xml file as follows:

<! Define three development environments -->
    <profiles>
        <profile>
            <! -- unique ids for different environments -->
            <id>dev</id>
            <activation>
                <! -- Activate the development environment by default -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <! --profile.active corresponds to @profile.active@--> in application.yml
                <profile.active>dev</profile.active>
            </properties>
        </profile>

        <! -- Test environment -->
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>

        <! -- Production environment -->
        <profile>
            <id>prod</id>
            <properties>
                <profile.active>prod</profile.active>
            </properties>
        </profile>
    </profiles>
Copy the code

The tag corresponds to @profile.active@ in the profile.

The

tag specifies the environment to be activated by default, which is the default environment when the -p option is not specified during packaging.

After the above configuration is complete, the following content will appear in the Maven TAB on the right side of IDEA:

You can choose the environment to package and then click Package.

Or you can package the project in the root directory with commands, but you need to specify the environment with -p, as follows:

mvn clean package package -P test
Copy the code

Profile activation conditions in Maven can also be activated depending on the JDK, operating system, and presence or absence of files. These are all configured in the

TAB as follows:

<! -- Activation specifies the activation mode, depending on the JDK environment, environment variables, and the presence or absence of files.
  <activation>
       <! -- Configure default activation -->
      <activeByDefault>true</activeByDefault>
                
      <! -- JDK version -->
      <! This profile is activated when the JDK version is 1.8 -->
      <jdk>1.8</jdk>
      <! -- This profile is active when JDK version 1.8 or above -->
      <jdk>[1.8,)</jdk>

      <! -- Depending on the current operating system -->
      <os>
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
      </os>
  </activation>
Copy the code

Resource filters

If you do not configure this step, you will pack all the configuration files in any environment, but we can configure to keep only the configuration files in the corresponding environment, which is more secure.

This step is easy to configure, just specify the

filter criteria in the pom.xml file, as follows:

<build>
  <resources>
  <! -- Exclude configuration files -->
    <resource>
      <directory>src/main/resources</directory>
      <! -- Exclude all configuration files -->
        <excludes>
          <! -- Exclude tag -- exclude tag -->
          <exclude>application*.properties</exclude>
        </excludes>
    </resource>

    <! Import configuration and files required for packaging according to activation conditions -->
    <resource>
      <directory>src/main/resources</directory>
      <! Import configuration files for the required environment -->
      <filtering>true</filtering>
      <includes>
        <include>application.yml</include>
          <! -- Import configuration file from Maven -->
        <include>application-${profile.active}.yml</include>
      </includes>
    </resource>
  </resources>
</build>
Copy the code

The above configuration is mainly divided into two aspects. The first is to exclude all configuration files, and the second is to dynamically import configuration files according to profile.active.

conclusion

At this point, Maven’s multi-environment packaging has been configured, which is relatively simple. You can select the environment packaging in IDEA, and also support the -p command to specify the environment packaging.

conclusion

This article describes two ways to package Spring Boot, each with its own pros and cons. Which one do you prefer?

Source code has been uploaded, reply keywords multi-environment configuration access.