Backend Development 8 min read

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.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Configuring Multiple Environments in Spring Boot and Maven

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=test

Or pass it as a command‑line argument when running the jar:

java -jar xxx.jar --spring.profiles.active=test

Maven multi‑environment configuration

Maven can also activate profiles that affect Spring Boot’s spring.profiles.active property.

Defining profiles in pom.xml

Example snippet:

dev
true
dev
test
test
prod
prod

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 :

src/main/resources
application*.properties
src/main/resources
true
application.yml
application-${profile.active}.yml

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.

JavaConfigurationmavenSpring Bootmulti-environmentProfiles
Code Ape Tech Column
Written by

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

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.