“This is my fourth day of the first Gwen Challenge 2022.First challenge in 2022”

The background,

In daily during the development process, we will have many sets of development environment, such as: the different application environment, such as development, test, production, these applications are corresponding to different configuration items, including different environment database address and port number are not the same, if there were no more than the environment of free switch, deployed is tedious and error-prone. This article mainly introduces how to configure multiple environments in the SpringBoot project.

Second, adopt Maven environment switching mode

In Maven project, we have a simple multi-environment configuration method. The idea of Maven is to isolate resource files according to the environment, and load the correct configuration resources to configure during the test. In addition, Maven’s multi-environment resource isolation configuration is well integrated with Jenkins CI.

1. Pom.xml configuration

Configure the build node of the POM.xml file first.

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<! Scan the file path of the replacement parameter -->
			</resource>
		</resources>
		<filters>
			<filter>src/main/filters/filter-${env}.properties</filter>
			<! -- The configuration mode of the environment filter, you need to create the corresponding file in this path -->
		</filters>

		<plugins>
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>${project.build.sourceEncoding}</encoding>
					<! Maven defaults to ${}, and Springbooot replaces this with @{} -->
					<useDefaultDelimiters>true</useDefaultDelimiters>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring-boot-maven-plugin.version}</version>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>
Copy the code

Configure properties in the POM.xml file

	<! ${attribute name} = ${attribute name}
<properties>
   	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

   	<! -- Plugin definition -->
   	<spring-boot-maven-plugin.version>2.1.6. RELEASE</spring-boot-maven-plugin.version>
</properties>
Copy the code

SRC /main/filters/filter-${env}. Properties is also marked as a resource file. See the following configuration for {env} values

Configure the Properties environment in the POM. XML file and switch the parameters of multiple environments

<! -- Different test environments -->
   <profiles>
   	<! -- Development environment, activated by default -->
   	<profile>
   		<id>dev</id>
   		<properties>
   			<env>dev</env>
   		</properties>
   		<activation>
   			<activeByDefault>true</activeByDefault><! -- Enable environment configuration by default -->
   		</activation>
   	</profile>

   	<! -- Test environment -->
   	<profile>
   		<id>test</id>
   		<properties>
   			<env>test</env>
   		</properties>
   	</profile>

   	<! -- Online environment -->
   	<profile>
   		<id>product</id>
   		<properties>
   			<env>product</env>
   		</properties>
   	</profile>
   </profiles>
Copy the code

2. Configuration files

Create the configuration file under SRC /main/filters:

  • Filter-dev. properties: development environment
  • Filter-product. properties: indicates the online environment
  • Filter-test. properties: indicates the test environment

Used to record environmental information, for example:

#EnvironmentEnvironment=dev host=http://127.0.0.1 port=8082 jdbc-URL = XXXX jdbc-username= XXXX jdbc-password= XXXXCopy the code

Create the application-maven.properties file under SRC /resource. This file records information about the parameters related to the environment switch, where variables can be configured using the form key=value. For example, the interface requests information about hosts and databases in different environments.

server.port=${port}

# Environment
Environment=${Environment}
Host.url=${host}

# data source configuration
spring.datasource.url=${jdbc-url}
spring.datasource.username==${jdbc-usernamel}
spring.datasource.password==${jdbc-password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Copy the code

As shown in figure:

At compile time, maven can specify the corresponding environment resource by adding the parameter -p command, such as -ptest, which will replace the parameter values in the test environment.

mvn clean install -DskipTests -Ptest
Copy the code

3. Jenkins configuration

Before Jenkins built the project tests using Maven, he tested them by using Maven locally. I’m going to parameterize the build here, but before parameterizing the build, I’ll explain how maven builds tests.Again, env corresponds to maven builds-P%env%And then the correspondingpom.xmlAdd the environment option to run

As follows:

clean test -U -DxmlFileName=%xmlFileName% -P%env%
Copy the code

Springboot multi-environment configuration

Profiles are Spring’s support for different configurations for different environments. Application -{profile}. Properties, {profile} corresponds to your environment identity. Such as:

  • Application-dev. properties: development environment
  • Application-test. properties: test environment
  • Application-product. properties: the online environment

Configure variables using the form key=value in configuration files of different environments.

server.port=8081
Copy the code

To specify which profile to execute, simply configure spring.profiles.active to the value corresponding to ${profile} in application.properties.

# set the environment to dev
spring.profiles.active=dev
Copy the code

Will be loadedapplication-dev.propertiesConfiguration content of

Four, summary

In general, we recommend Maven environment switch when doing automated test integration execution, because dynamic environment switch can be done, and SpringBoot multi-environment configuration is convenient to use when using IDE development.

Source code:

  • Github.com/zuozewei/bl…