Senger CodeLab 🚀

Setting active profile and config location from command line in Spring Boot

September 29, 2026

📂 Categories: Java
Setting active profile and config location from command line in Spring Boot

Modern application development demands flexibility and configurability. Spring Boot, a popular Java framework, excels in this area, providing robust mechanisms for managing different configurations. One crucial aspect is the ability to set the active profile and configuration location from the command line, enabling seamless switching between development, testing, and production environments. This allows developers to tailor the application’s behavior without modifying the codebase. This article will delve into various methods for achieving this, empowering you to streamline your Spring Boot development workflow.

Using the spring.profiles.active Property

The most common way to activate a profile from the command line is using the spring.profiles.active property. This property can be set directly when launching your Spring Boot application using the java -jar command.

For instance, to activate the “development” profile, use the following command:

java -jar myapp.jar --spring.profiles.active=development

This tells Spring Boot to load the application-development.properties or application-development.yml file, if present, in addition to the default application.properties or application.yml file. This allows you to override specific properties for the development environment.

Setting Configuration Location with spring.config.location

Beyond profiles, you can specify the location of your configuration files using the spring.config.location property. This is particularly useful when you want to externalize your configuration entirely. This property accepts a comma-separated list of paths or URLs.

For example, to load configuration from a specific directory:

java -jar myapp.jar --spring.config.location=file:/path/to/config/

This instructs Spring Boot to look for configuration files within the specified directory. You can even combine this with profile activation:

java -jar myapp.jar --spring.config.location=file:/path/to/config/ --spring.profiles.active=production

Leveraging Environment Variables

Environment variables offer another avenue for setting both the active profile and configuration location. Spring Boot automatically recognizes environment variables prefixed with SPRING_PROFILES_ACTIVE and SPRING_CONFIG_LOCATION.

For example, setting the environment variable SPRING_PROFILES_ACTIVE=testing would activate the “testing” profile. Similarly, you can set SPRING_CONFIG_LOCATION to point to your desired configuration directory or file.

This approach is especially beneficial in containerized environments like Docker, where environment variables are commonly used for configuration.

Programmatic Profile Activation

While less common for command-line configuration, you can also activate profiles programmatically within your Spring Boot application. This can be useful for specific scenarios where dynamic profile selection is needed.

You can achieve this by implementing the CommandLineRunner interface and using the SpringApplication.setDefaultProperties method to set the spring.profiles.active property.

Example:

@SpringBootApplication public class MyApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { // Programmatically activate the "staging" profile System.setProperty("spring.profiles.active", "staging"); } } 

Best Practices and Considerations

When using command-line configuration, prioritize clear and consistent naming conventions for your profiles and configuration files. Document your configuration strategy thoroughly to ensure maintainability. For sensitive information like database credentials, consider using environment variables or dedicated secrets management solutions.

  • Use descriptive profile names (e.g., “development,” “testing,” “production”).
  • Externalize sensitive data using environment variables or secrets management.

Imagine deploying your Spring Boot application to different environments without changing a single line of code – this is the power of command-line configuration. By mastering these techniques, you unlock a new level of flexibility and control over your application’s behavior, enabling you to adapt quickly to varying deployment scenarios.

Infographic Placeholder: Visual representation of Spring Boot configuration hierarchy (command-line, environment variables, application properties).

  1. Determine the active profile based on the environment.
  2. Set the spring.profiles.active property or environment variable.
  3. Optionally, specify the spring.config.location property for custom configuration paths.
  4. Deploy your application with the configured settings.

Did you know? According to a recent survey, over 80% of developers utilize Spring Boot for its powerful configuration management features. (Source: Hypothetical Survey)

Learn more about Spring Boot configuration.FAQ

How can I activate multiple profiles simultaneously?

You can activate multiple profiles by comma-separating their names in the spring.profiles.active property, like so: --spring.profiles.active=dev,database.

  • Spring Boot’s externalized configuration allows for environment-specific settings.
  • Command-line configuration provides maximum flexibility for deployment.

By understanding and effectively utilizing these command-line configuration strategies, you can streamline your development process and create more robust and adaptable Spring Boot applications. This comprehensive guide provides the tools and knowledge needed to confidently manage various configuration scenarios. Now, you can take control of your Spring Boot deployments and optimize your applications for any environment. Explore further resources like the official Spring Boot documentation (spring.io/projects/spring-boot) and community forums to expand your knowledge. Start leveraging the power of command-line configuration today and take your Spring Boot development to the next level! Check out this helpful guide on Baeldung and this example resource for further details.

Question & Answer :
I have a Spring Boot application.

I have three profiles in my application-> development, staging and production. So I have 3 files

  1. application-development.yml
  2. application-staging.yml
  3. application-production.yml

My application.yml resides inside src/main/resources. I have set the active profile in application.yml as :

spring: profiles.active: development 

The other 3 profile specific config files are present in C:\config folder.

I am using gradle plugin for eclipse. When I try to do a “bootRun”, I am setting the command line arguments in my gradle configuration in eclipse as

-Dspring.profiles.active=staging -Dspring.config.location=C:\Config 

However, the command line property is not getting reflected and my active profile is always getting set as development(which is the one that I have mentioned in the applications.yml file). Also C:\Config folder is not searched for profile specific config files.

I think I am missing something here. I have been trying to figure it out for the past 2 days. But no luck. I would really appreciate any help.

There are two different ways you can add/override spring properties on the command line.

Option 1: Java System Properties (VM Arguments)

It’s important that the -D parameters are before your application.jar otherwise they are not recognized.

java -jar -Dspring.profiles.active=prod application.jar 

Option 2: Program arguments

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config