Setting active profile and config location from command line in spring boot

432,220

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

Share:
432,220

Related videos on Youtube

Vinod Mohanan
Author by

Vinod Mohanan

Updated on March 09, 2022

Comments

  • Vinod Mohanan
    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

    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.

    • Biju Kunjummen
      Biju Kunjummen almost 9 years
      Can you please add your bootRun command line also
    • Vinod Mohanan
      Vinod Mohanan almost 9 years
      I 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
    Vinod Mohanan almost 9 years
    Thank 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
    jst almost 9 years
    You should follow the advice given in this question to pass the args to bootRun stackoverflow.com/questions/25079244/…
  • Vinod Mohanan
    Vinod Mohanan almost 9 years
    Thank you. That really helped.
  • James Watkins
    James Watkins about 8 years
    This causes error "Unrecognized option: --spring.config.location"
  • Xdg
    Xdg about 8 years
    -D is the correct way to set Java System properties. --something is a bash parameter.
  • edufinn
    edufinn about 8 years
    This 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 to systemProperty map.
  • Pushkar
    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
    martin over 7 years
    Order of the -D parameters is really important :)
  • Olivier Boissé
    Olivier Boissé over 7 years
    This also works for me when using Run As -> Java Application in Eclipse
  • user1767316
    user1767316 over 7 years
    this seems to be a gradle only solution, is there no genric solution ?
  • prayagupa
    prayagupa about 7 years
    how 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
    best wishes about 7 years
    @prayagupd yes, you can have system properties set in your bash_profile.
  • prayagupa
    prayagupa about 7 years
    @maneesh yeah, I am using env variable SPRING_PROFILES_ACTIVE exported via ~/.bash_profile. export SPRING_PROFILES_ACTIVE=e2e
  • ben3000
    ben3000 about 6 years
    Yes, and this avoids the need to muck around with the way Gradle hands system properties through to the application.
  • Igor Donin
    Igor Donin about 6 years
    This 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
    sheldonkreger almost 6 years
    Can 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
    Scala Enthusiast almost 6 years
    Where exactly are you adding this? Anywhere in the build.gradle file or in a specific location within the file?
  • kelgwiin
    kelgwiin over 5 years
    If 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 the spring.profiles.active and set the value that your desire.
  • Dexter Legaspi
    Dexter Legaspi over 5 years
    actually 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 OR java your-spring-boot.jar --spring.profiles.active=staging --spring.config.location=C:\Config
  • rwenz3l
    rwenz3l about 5 years
    has changed to mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar, see: docs.spring.io/spring-boot/docs/current/maven-plugin/example‌​s/…
  • Pramod Setlur
    Pramod Setlur almost 5 years
    When 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/propert‌​ies/application-$ENV‌​.properties /code/app.jar
  • smilyface
    smilyface almost 5 years
    You mean spring / spring-boot ? (Spring 1x and Spring 2x) !
  • Sma Ma
    Sma Ma almost 5 years
    @smilyface spring boot. spring boot is also available in different versions: mvnrepository.com/artifact/org.springframework.boot/spring-b‌​oot
  • Donghua Liu
    Donghua Liu over 4 years
    I use spring-boot 2.1.3, and -Dspring-boot.run.profiles=local did not work, -Dspring.profiles.active=local worked.
  • John Smith
    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
    Grigory Kislin about 4 years
    spring-boot 2.2.0: work both: -Dspring-boot.run.profiles and -Dspring.profiles.active
  • vinicius gati
    vinicius gati over 3 years
    @user1767316 you can set a environment variable with SPRING_PROFILES_ACTIVE=profile, works like a charm
  • Dawngerpony
    Dawngerpony over 3 years
  • Roeland Van Heddegem
    Roeland Van Heddegem over 2 years
    Option 1 didn't work for me until I added quotes: java -jar -D"spring.profiles.active"=prod application.jar