Setting active profile and config location from command line in spring boot
Solution 1
I had to add this:
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"
}
And now bootRun picks up the profile and config locations.
Thanks a lot @jst for the pointer.
Solution 2
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
Solution 3
My best practice is to define this as a VM "-D" argument. Please note the differences between spring boot 1.x and 2.x.
The profiles to enable can be specified on the command line:
Spring-Boot 2.x (works only with maven)
-Dspring-boot.run.profiles=local
Spring-Boot 1.x
-Dspring.profiles.active=local
example usage with maven:
Spring-Boot 2.x
mvn spring-boot:run -Dspring-boot.run.profiles=local
Spring-Boot 1.x and 2.x
mvn spring-boot:run -Dspring.profiles.active=local
Make sure to separate them with a comma for multiple profiles:
mvn spring-boot:run -Dspring.profiles.active=local,foo,bar
mvn spring-boot:run -Dspring-boot.run.profiles=local,foo,bar
Solution 4
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
is not correct.
should be:
--spring.profiles.active=staging --spring.config.location=C:\Config
Solution 5
There's another way by setting the OS environment variable, SPRING_PROFILES_ACTIVE.
for eg :
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
Reference : How to set active Spring profiles
Related videos on Youtube
Vinod Mohanan
Updated on March 09, 2022Comments
-
Vinod Mohanan about 2 years
I have a spring boot application.
I have three profiles in my application-> development, staging and production. So I have 3 files
- application-development.yml
- application-staging.yml
- 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.
-
Biju Kunjummen almost 9 yearsCan you please add your
bootRun
command line also -
Vinod Mohanan almost 9 yearsI was running it from eclipse and not command line till now. But I tried running from using "gradle bootRun -Dspring.config.location=C:\Config\ -Dspring.profiles.active=staging" and got the same result.
-
Vinod Mohanan almost 9 yearsThank you for pointing it out. However, when I run -Dspring.profiles.active=staging -Dspring.config.location=C:\Config\ also gives me the same issue. Even active profile is not getting reflected. I think for some reason my command line is not getting passed over.
-
jst almost 9 yearsYou should follow the advice given in this question to pass the args to bootRun stackoverflow.com/questions/25079244/…
-
Vinod Mohanan almost 9 yearsThank you. That really helped.
-
James Watkins about 8 yearsThis causes error "Unrecognized option: --spring.config.location"
-
Xdg about 8 years-D is the correct way to set Java System properties. --something is a bash parameter.
-
edufinn about 8 yearsThis can be even more simpler as following:
bootRun { systemProperties = System.properties }
. This command will copy all parameters passed with-D
switch with the same keys tosystemProperty
map. -
Pushkar about 8 years--spring.profiles.active work for me, same thing I referred from docs.spring.io/spring-boot/docs/current/reference/html/…
-
martin over 7 yearsOrder of the -D parameters is really important :)
-
Olivier Boissé over 7 yearsThis also works for me when using Run As -> Java Application in Eclipse
-
user1767316 over 7 yearsthis seems to be a gradle only solution, is there no genric solution ?
-
prayagupa about 7 yearshow can you achieve this while deploying to say tomcat container? In that case I simply put my war to webapps folder of tomcat, how do I provide the profile info? by setting system properties?
-
best wishes about 7 years@prayagupd yes, you can have system properties set in your bash_profile.
-
prayagupa about 7 years@maneesh yeah, I am using env variable
SPRING_PROFILES_ACTIVE
exported via~/.bash_profile
.export SPRING_PROFILES_ACTIVE=e2e
-
ben3000 about 6 yearsYes, and this avoids the need to muck around with the way Gradle hands system properties through to the application.
-
Igor Donin about 6 yearsThis is the neat way. It should also be used to set database users and passwords and other sensitive configurations so they're not checked in version control.
-
sheldonkreger almost 6 yearsCan anybody clarify how to tell what relative path IntelliJ is using when it runs this command? For example, if I didn't want to use an absolute path and instead specify --spring.config.location=my/relative/path/config/
-
Scala Enthusiast almost 6 yearsWhere exactly are you adding this? Anywhere in the build.gradle file or in a specific location within the file?
-
kelgwiin over 5 yearsIf you're using Intellij and you want override the spring variables so you should go to
edit Configuration -> Spring Boot Config -> Overrride parameters
and search for thespring.profiles.active
and set the value that your desire. -
Dexter Legaspi over 5 yearsactually BOTH are correct, it depends on how it is used: it can be
java -Dspring.profiles.active=staging -Dspring.config.location=C:\Config your-spring-boot-app.jar
ORjava your-spring-boot.jar --spring.profiles.active=staging --spring.config.location=C:\Config
-
rwenz3l about 5 yearshas changed to
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
, see: docs.spring.io/spring-boot/docs/current/maven-plugin/examples/… -
Pramod Setlur almost 5 yearsWhen you say the order is important: Can we pass in both args: -Dspring.profile.active and -Dspring.config.location such that, the profile is set according to the first argument and the property file is picked up according to the second args? Eg:
java -Dspring.profiles.active=$ENV -Dspring.config.location=file:///aws-secrets-manager/properties/application-$ENV.properties /code/app.jar
-
smilyface almost 5 yearsYou mean spring / spring-boot ? (Spring 1x and Spring 2x) !
-
Sma Ma almost 5 years@smilyface spring boot. spring boot is also available in different versions: mvnrepository.com/artifact/org.springframework.boot/spring-boot
-
Donghua Liu over 4 yearsI use spring-boot 2.1.3, and
-Dspring-boot.run.profiles=local
did not work,-Dspring.profiles.active=local
worked. -
John Smith over 4 years@rwenz3l Thanks! that works for me, just upgraded a project from Spring Boot 1 to 2. Now I just add them all in my bashrc...
springmvn="mvn clean spring-boot:run -Dspring.profiles.active=local -Dspring-boot.run.profiles=local"
-
Grigory Kislin about 4 yearsspring-boot 2.2.0: work both:
-Dspring-boot.run.profiles
and-Dspring.profiles.active
-
vinicius gati over 3 years@user1767316 you can set a environment variable with SPRING_PROFILES_ACTIVE=profile, works like a charm
-
Dawngerpony over 3 yearsDocs for this answer: docs.spring.io/spring-boot/docs/current/maven-plugin/reference/…
-
Roeland Van Heddegem over 2 yearsOption 1 didn't work for me until I added quotes: java -jar -D"spring.profiles.active"=prod application.jar