Configuring Multiple Environments in Spring Boot and Maven
This article explains how to set up separate configuration files for development, testing, and production environments in Spring Boot, how to activate the desired profile via properties or command line, and how to use Maven profiles and resource filtering to package the application accordingly.
In daily development there are usually three environments: development (dev), testing (test), and production (prod), each with different configurations such as database, port, and IP address.
This article explains how to configure multiple environments in Spring Boot and how to package the application accordingly.
Spring Boot built‑in multi‑environment configuration
Spring Boot supports profile‑based configuration out of the box, allowing you to switch environments at build or run time.
Creating separate configuration files
Create three property files named application-dev.properties, application-test.properties and application-prod.properties. The base file application.properties also remains in the project.
All four files coexist in the project.
Specifying the active profile
You can set the active profile either in application.properties / application.yml:
# Set active profile to test
spring.profiles.active=testOr pass it as a command‑line argument when running the jar:
java -jar xxx.jar --spring.profiles.active=testMaven multi‑environment configuration
Maven can also activate profiles that affect Spring Boot’s spring.profiles.active property.
Defining profiles in pom.xml
Example snippet:
<!-- Define three development profiles -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>The variable profile.active is supplied with -P test (or dev/prod) during the Maven build.
Resource filtering
To include only the configuration file for the selected profile, configure resource filtering in pom.xml:
<build>
<resources>
<!-- Exclude all configuration files first -->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
<!-- Include the required configuration file -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profile.active}.yml</include>
</includes>
</resource>
</resources>
</build>After these settings you can select the profile in IDEA or run mvn clean package -P test to build the jar for the test environment.
Conclusion
The article demonstrates two ways to package a Spring Boot application with environment‑specific configurations, using either Spring Boot’s built‑in profile mechanism or Maven profiles, each with its own advantages.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
