Setting active profile and config location from command line in Spring Boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot lets you change profile selection and configuration file loading at startup without rebuilding the application. Two of the most common command-line properties are spring.profiles.active and the configuration-location properties.
The detail that matters is whether you want to replace Boot's default config search locations or add extra ones on top of them. Many deployment mistakes come from choosing the wrong property for that distinction.
Activate Profiles From the Command Line
The standard way to activate a profile is:
This tells Spring Boot to load profile-specific configuration such as application-prod.yml or application-prod.properties in addition to the shared base configuration.
You can activate multiple profiles too:
That is useful when one profile represents the environment and another enables an orthogonal feature set.
Replace Config Search Locations With spring.config.location
If your config should come from a specific external place instead of the default search paths, use:
This replaces the default config search locations. If the target is a directory, keep the trailing slash so Boot treats it as a directory and not as a file path.
You can also point to a specific file:
Add Extra Locations With spring.config.additional-location
If you want to keep Boot's defaults and add another location on top, use:
This is often the safer production choice because the packaged defaults still participate in resolution, while the external directory provides overrides or additions.
The difference is important:
- '
spring.config.locationreplaces default search locations' - '
spring.config.additional-locationextends them'
Combine Profile and Location Cleanly
A common startup pattern is:
This keeps the normal packaged config, adds external overrides, and still applies profile-specific resolution in the combined search set.
That is a clean deployment model when operations wants environment-specific config outside the jar but the application still ships with sensible defaults.
Command-Line Arguments Override Other Sources Easily
Spring Boot also supports environment variables such as:
But command-line arguments usually have very high precedence, so this:
will override the environment setting. That is convenient for one-off runs, but it also means startup scripts can override environment-level configuration more easily than some teams expect.
Common Pitfalls
The biggest mistake is using spring.config.location when the real intent was only to add another external directory. That can accidentally stop Boot from loading the normal packaged defaults.
Another common issue is forgetting the trailing slash on a directory location, which can make Boot interpret the argument differently from what you intended.
Developers also activate multiple profiles without thinking through how overlapping properties should resolve. Profile composition is powerful, but it still needs a clear convention.
Finally, do not assume environment variables and command-line arguments behave the same in precedence. Command-line startup flags are often stronger than people realize.
That makes startup scripts powerful, but it also makes them a frequent source of accidental overrides.
Summary
- Use
--spring.profiles.active=...to choose one or more active profiles at startup. - Use
--spring.config.location=...when you want to replace the default config search path. - Use
--spring.config.additional-location=...when you want to add external config while keeping the defaults. - Combine profiles and additional locations for clean environment-specific deployments.
- Be explicit about precedence so command-line overrides do not surprise you later.
Related reading
- Setting active profile and config location from command line in Spring Boot
- Setting default values for columns in JPA
- Setting Precedence of Multiple ControllerAdvice ExceptionHandlers
- Setting Short Value Java
- Setting Spring Profile variable
- Setting the default active profile in Spring-boot
- Setting the default Java character encoding
- Shaded/Repackaged jar as a dependency

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.