Introduction
In Spring Boot, managing different configurations across various environments (like development, staging, production) is a common practice. This often involves using profiles to segregate settings specific to each environment. Moreover, you might want to change the default locations of configuration files. This tutorial will guide you through setting active profiles and customizing config file locations from the command line in Spring Boot applications.
Understanding Profiles
Spring Boot allows you to define different configurations for various environments using profiles. These are typically named (e.g., development
, staging
, production
) and correspond to configuration files such as application-development.yml
, application-staging.yml
, and application-production.yml
.
Default Configuration Setup
-
Default Profile: By default, Spring Boot searches for a file named
application.yml
or properties under thesrc/main/resources
directory. -
Profiles Activation: The active profile can be set in the
application.yml
using:spring: profiles.active: development
Customizing Profiles and Configurations from Command Line
Spring Boot offers multiple ways to override these settings via command line, which is particularly useful when running your application in different environments without changing the code.
Method 1: Using Java System Properties (VM Arguments)
Java system properties are an effective way to specify profiles and configuration locations. This method involves setting VM arguments before launching the Spring Boot jar:
java -jar -Dspring.profiles.active=staging -Dspring.config.location=file:C:\Config your-application.jar
Key Points:
- The
-D
options must precedeyour-application.jar
. - Use
file:
prefix to specify a file path when setting config locations.
Method 2: Using Program Arguments
Alternatively, Spring Boot supports passing profile and configuration settings as program arguments:
java -jar your-application.jar --spring.profiles.active=staging --spring.config.location=file:C:\Config
Key Points:
- This method is intuitive and aligns with the Spring Boot command-line style.
- Ensure correct syntax by using double dashes (
--
).
Method 3: Using Gradle or Maven
When working within a build automation tool like Gradle or Maven, you can set profiles directly in your run configurations:
For Gradle:
bootRun {
String activeProfile = System.properties['spring.profiles.active']
String confLoc = System.properties['spring.config.location']
systemProperty "spring.profiles.active", activeProfile
systemProperty "spring.config.location", "file:$confLoc"
}
For Maven:
For Spring Boot 1.x:
mvn spring-boot:run -Dspring.profiles.active=staging
For Spring Boot 2.x (using specific run profiles):
mvn spring-boot:run -Dspring-boot.run.profiles=staging
Note: Separate multiple profiles with commas if needed.
Method 4: Using Environment Variables
You can also set the active profile using an OS environment variable:
-
For Windows:
set SPRING_PROFILES_ACTIVE=dev && gradle clean bootRun
This approach is useful for integrating into CI/CD pipelines or scripting environments.
Best Practices and Tips
- Consistency: Use consistent naming conventions across profiles to avoid confusion.
- Documentation: Document the profile activation process in your project’s README or development guides.
- Testing: Always test configurations in a non-production environment first to ensure correctness.
- Security: Be cautious with config locations, especially if they contain sensitive information.
Conclusion
Configuring active profiles and customizing configuration file locations is crucial for managing Spring Boot applications across different environments. By utilizing command-line arguments, program arguments, or build tool configurations, you can flexibly adapt your application to various contexts without altering the codebase.