Based on Maven, control spring boot configuration file gracefully

Posted by yujikaido on Sun, 24 Nov 2019 22:46:19 +0100

background

As we all know, in the process of software development, we usually have production environment, development environment, test environment and so on. For these environments, we need to set different values for the same parameters in the configuration file. Then, how to switch simply, elegantly and quickly?

Solution

I. use the Profile provided by SpringBoot natively

First of all, we know that spring boot provides:

spring:  
 profiles:
  active: pro

This property is used to select the configuration file of the corresponding environment. Of course, multiple configuration files can also be set at the same time:

spring:  
 profiles:
  active: 
   - pro
   - test

But there is a problem. This method is not fast and simple enough. You need to modify the parameter value every time you pack. Or you need to increase the parameter -- spring.profiles.active when you start. It's too cumbersome, so what can be simpler.

II. Combination of Maven's Profile and SpringBoot's Porfile

We usually use Maven as a project construction tool, and in maven, we also provide the function of customized configuration information, namely Profile.

Our idea is as follows: when Maven is packing, we can control the Profile of SpringBoot by setting the parameters of Maven.

The configuration is as follows:

#Only the main configuration is given here

<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<druid.profiles.active>druid-dev</druid.profiles.active>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>pro</id>
			<properties>
				<druid.profiles.active>druid-pro</druid.profiles.active>
			</properties>
		</profile>
</profiles>

<resources>
			<resource>
				<directory>src/main/resources</directory>
                 #This must be added, otherwise it will not be replaced
				<filtering>true</filtering>
			</resource>
</resources>

Then, the yml configuration file of SpringBoot is as follows:

spring:  
 profiles:
  active: @druid.profiles.active@

When we install (package), we only need to set - P dev, so we can set it as the configuration file of dev environment.

Other

Make the packed jar package only contain the configuration file of the corresponding environment. Add the following configuration in the pom file of Maven:

<resources>
#Exclude all yml files in this resource
			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>application*.yml</exclude>
				</excludes>
			</resource>
#In this resource, the corresponding configuration file is imported according to the profile
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>application.yml</include>
#Import the corresponding configuration file according to the profile
					<include>application-${druid.profiles.active}.yml</include>
				</includes>
			</resource>
</resources>

Topics: Druid Maven Spring SpringBoot