Generally speaking , Project development is divided into development environment (dev)
、 Test environment (test)
、 Preproduction environment (pre)
、 Production environment (prod)
. There are some differences in configuration between different environments , We switch according to different deployment environment files , Let's talk about how to configure different environments .
Configuration file preparation
Prepare the corresponding application-*.properties
file .
- application.properties: Common configuration
- application-dev.properties: Development environment configuration
- application-test.properties: Test environment configuration
- application-prod.properties: Production environment configuration
pom.xml To configure
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<!-- Whether to replace @[email protected] It means maven properties Property value -->
<filtering>true</filtering>
</resource>
</resources>
application.properties
# At this point, the configuration is the development environment
spring.profiles.active=dev
# The test environment is configured at this time
spring.profiles.active=test
# At this point, the configuration is the production environment
spring.profiles.active=prod
The above configuration requires us to modify the corresponding values in different environments , This is very inconvenient , At this point, we can configure it by variables , Now let's make some changes .
pom.xml Add
<profiles>
<!-- development environment -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<!-- Test environment -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<!-- Production environment -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
application.properties It is amended as follows
# At this point through env To distinguish between different environments
[email protected]@
At this time we are IDEA On the right you see the following :
Here we can choose the corresponding environment
Now we can switch Profiles To switch the environment . But in this case, it can only be started by running the startup class , like this :
Only in this way can it be applied to Profiles Selected value .
spring-boot:run Mode start
It can't be applied to IDEA On the right Profiles Environmental value of .
- mvn spring-boot:run -P dev
We need to add the specified environment after the command , like this :mvn spring-boot:run -P dev
Specified in dev Start up in the environment of , among -P
Appoint pom.xml Corresponding profiles Value . here application.properties The configuration file is no longer valid .
springboot1.x start-up
- mvn spring-boot:run -Drun.profiles=dev
springboot2.x start-up
- mvn spring-boot:run -Dspring-boot.run.profiles=test
function jar package
java -jar -Dspring.profiles.active=test demo.jar
# perhaps
java -jar --spring.profiles.active=test demo.jar