Setting active profile and config location from command line in Spring Boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In a Spring Boot application, the ability to dynamically change the configuration using profiles and external configuration files is a powerful tool. This flexibility enables developers and operators to adjust application behavior without altering code, making it ideal for different environments such as development, testing, and production. This article explores how to set an active profile and specify a configuration file location directly from the command line in a Spring Boot application.
Understanding Profiles in Spring Boot
Spring Boot profiles provide a way to segregate parts of your application configuration, enabling it to be tailored for various environments. By isolating configurations, you can execute an application with different settings for development, testing, and production use cases.
Profiles are specified in the application.properties or application.yml files, and you can activate one or more profiles via command-line arguments, environment variables, or programmatically.
Example:
Command-Line Profiles Configuration
To activate a profile from the command line, you can use the --spring.profiles.active property. This approach overrides any profile settings defined in your application.* files.
Syntax:
Setting Configuration Location from the Command Line
In certain scenarios, you might need to load a configuration file from a non-default location. Spring Boot allows you to specify a custom location using the --spring.config.location argument. This argument informs the application to look for the configuration file in the specified path.
Syntax:
Using Multiple Configurations and Profiles
You can use multiple profiles and configuration files concurrently by comma-separating the values. Spring Boot will handle this by merging the configurations, with higher priority given to the properties specified last.
Example:
Comprehensive Command-Line Setup
Combining both profiles and configuration locations, you can start your Spring Boot application with a command line that specifies a complete custom setup. This facilitates a highly adaptable deployment strategy without the need to alter source code or rebuild the application.
Example of Complete Command:
Example Execution
Assuming you have the following files:
/configs/development.ymlfor the development profile/configs/production.ymlfor the production profile
Example commands:
Priority and Override Fallback
- Order of File Processing: When using multiple configuration files, Spring Boot processes them in the order specified. Properties in later files can override those in earlier ones.
- Profile Specific Files: Profile-specific configurations such as
application-dev.propertiesoverride the default configuration file but are themselves overridden by the command-line arguments.
Summary Table
| Feature | Description | Command Syntax Example |
| Activate Profile | Set active profile to change application behavior across environments | --spring.profiles.active=dev |
| Custom Config Location | Specify a custom config file path | --spring.config.location=/path/to/config.properties |
| Multiple Profiles | Use multiple profiles concurrently | --spring.profiles.active=dev,qa |
| Multiple Configs | Load multiple config files | --spring.config.location=file:/config1.properties,file:/config2.yml |
| Comprehensive Setup | Combine profiles and custom configs for full environment setup | --spring.profiles.active=prod --spring.config.location=/path/to/conf |
Additional Considerations
- Environment Variables: Alternatively, profiles and configuration paths can be set using environment variables like
SPRING_PROFILES_ACTIVEandSPRING_CONFIG_LOCATION. - Configuration Precedence: Remember that command-line arguments always take precedence over environment variables and application properties files.
With these capabilities, Spring Boot facilitates a highly customizable application configuration out of the box, allowing flexibility in deployment and development processes. This adaptability is critical for modern software development practices, emphasizing ease of deployment, DevOps integration, and microservices architecture.

