Spring Boot
Command Line
Configuration
Active Profile
Application Properties

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:

properties
# application.properties
spring.profiles.active=dev

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:

bash
$ java -jar myapp.jar --spring.profiles.active=dev

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:

bash
$ java -jar myapp.jar --spring.config.location=/path/to/my/application.properties

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:

bash
$ java -jar myapp.jar --spring.profiles.active=dev,staging --spring.config.location=file:/path/to/dev.conf,file:/path/to/staging.conf

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:

bash
$ java -jar myapp.jar --spring.profiles.active=production --spring.config.location=file:/external/config/application-production.properties

Example Execution

Assuming you have the following files:

  • /configs/development.yml for the development profile
  • /configs/production.yml for the production profile

Example commands:

bash
1# Run with development profile
2$ java -jar myapp.jar --spring.profiles.active=development --spring.config.location=file:/configs/development.yml
3
4# Run with production profile
5$ java -jar myapp.jar --spring.profiles.active=production --spring.config.location=file:/configs/production.yml

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.properties override the default configuration file but are themselves overridden by the command-line arguments.

Summary Table

FeatureDescriptionCommand Syntax Example
Activate ProfileSet active profile to change application behavior across environments--spring.profiles.active=dev
Custom Config LocationSpecify a custom config file path--spring.config.location=/path/to/config.properties
Multiple ProfilesUse multiple profiles concurrently--spring.profiles.active=dev,qa
Multiple ConfigsLoad multiple config files--spring.config.location=file:/config1.properties,file:/config2.yml
Comprehensive SetupCombine 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_ACTIVE and SPRING_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.


Course illustration
Course illustration

All Rights Reserved.