1. Yml multi-environment configuration
In Spring Boot, the multi-environment configuration file name must meet the application-{profile}. Yml format, where {profile} corresponds to your environment identifier.
application-dev The development environment
application-test The test environment
application-prod The production environment
Copy the code
If we want to activate an environment, we just need to activate it in application.yml:
spring:
profiles:
active: dev
Copy the code
In addition, suppose we configure some basic Settings like:
application-dev.yml
server:
port: 9001
Copy the code
application-test.yml
server:
port: 9002
Copy the code
application-prod.yml
server:
port: 9003
Copy the code
At this point, when we modify application.yml:
- Instead of
dev
- Instead of
test
- Instead of
prod
2. Create a multi-environment profile package
With the above steps, it is easy to switch the current environment, but it is a little more troublesome. Is there any configuration file that can be used instead of manually changing the profile and creating a multi-environment profile package?
The answer is yesCopy the code
pom.xml
Add a profile node to the POM file and a profile rule for package filtering to the Resources node under Build
<profiles>
<profile>
<! -- Development environment -->
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<! -- Default active environment -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<! -- Test environment -->
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<! -- Production environment -->
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.yml</include>
<include>application.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Copy the code
Configure a dynamic property in application.yml for placeholder. The default delimiter is @ property name @. This property will be replaced by an argument passed into maven packaging.
spring:
profiles:
active: @profileActive@
Copy the code
The visual selection environment on the right makes work more efficient;
Maven multi-environment packaging
The package-filtering profile rules are also filled with a placeholder, which is also replaced by parameters passed in by Maven during packaging.
- 1.
Run the -d command to pass in the profileActive attribute value
, such as:
clean install -Dmaven.test.skip=true -DprofileActive=dev
Copy the code
- 2,
Run the -p command to specify a profile environment
, such as:
clean package -P prod
Copy the code
Visual selection on the right is more convenient:
🆗 Without further ado, well, that concludes this little chapter;
Code repository address: Click to enter