When we conduct a multi-project environment configuration, there are many ways for us to choose, such as SpringBoot own application-dev.yml, maven's profile and so on. Presented here is how to take advantage of multi-environment configuration profile.
First, you need to add profile configuration in pom.xml:
<profiles>
<!-- 开发环境 默认激活-->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>publish</id>
<properties>
<env>publish</env>
</properties>
</profile>
<!-- 本地环境 -->
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
<!--默认启用-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
profiles arranged inside the plurality of profile files, i.e. dev, publish, local environment, <env> tag to switch to a different environment, the environment needs to activate. <ActiveByDefault> is set to true indicates that: the default profile is active.
Next, we want resource of filtering tag is set to true, it indicates that enable parametric values to replace the mark, while the parametric values derived from the properties file filter tab. The following is the original explanation:
Whether resources are filtered to replace tokens with parameterised values or not.
The values are taken from the <code>properties</code> element and from the properties in the files listed
in the <code>filters</code> element.
<build>
<!-- 指定使用filter -->
<filters>
<filter>src/main/resources/profiles/${env}/env.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<excludes>
<exclude>profiles/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>
filter is referenced in the env attribute profile, which represents the value of the environment variable to read, different properties files configured with different values of the environment:
Speaking in front of <filtering> true </ filtering> represents the value of the parameter enable to replace the mark, what does it mean? Let's look at how application.yml to be represented?
server:
port: 8080
tomcat:
max-threads: 800
uri-encoding: UTF-8
spring:
redis:
host: ${spring.redis.host}
timeout: ${spring.redis.timeout}
pool:
max-idle: ${spring.redis.pool.max-idle}
max-active: ${spring.redis.pool.max-active}
password: ${spring.redis.password}
database: ${spring.redis.database}
Thus, the whole process should be carried out by: maven compile command execution, <resource> read <filter> properties of an attribute value, and then replace the up / down resources src / main marker in - such as $ {spring.redis.host} These ones.
Finally, only one question, how switching environment? If you develop the tools IDEA, directly next to the window switches to:
If you use the command-line compiler, plus -P selected profile can be as follows:
clean -U package -P dev -DskipTests=true -f pom.xml