308

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Vinod Mohanan
  • 3,729
  • 2
  • 17
  • 25
  • Can you please add your `bootRun` command line also – Biju Kunjummen Jun 24 '15 at 23:17
  • 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 Jun 25 '15 at 13:23

16 Answers16

596

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RenRen
  • 10,550
  • 4
  • 37
  • 39
  • 85
    Order of the -D parameters is really important :) – martin Dec 19 '16 at 08:58
  • 4
    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? – prayagupa Mar 27 '17 at 07:36
  • @prayagupd yes, you can have system properties set in your bash_profile. – best wishes May 17 '17 at 03:29
  • 3
    @maneesh yeah, I am using [env variable](https://github.com/prayagupd/eccount-rest/tree/REST-API-load-balancing) `SPRING_PROFILES_ACTIVE` exported via `~/.bash_profile`. `export SPRING_PROFILES_ACTIVE=e2e` – prayagupa May 17 '17 at 07:35
  • 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/ – sheldonkreger Jun 09 '18 at 18:28
  • 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. – kelgwiin Jan 19 '19 at 12:11
  • 1
    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/properties/application-$ENV.properties /code/app.jar` – Pramod Setlur Jun 12 '19 at 21:09
  • 2
    Option 1 didn't work for me until I added quotes: **java -jar -D"spring.profiles.active"=prod application.jar** – Roeland Van Heddegem Jan 31 '22 at 16:50
  • When you deploy your spring boot WAR app to tomcat, modify the **tomcat_home/conf/catalina.properties** by adding following properties **spring.profiles.active=dev/qa/prod**. After the changes restart the tomcat server. – Panda1667075 Aug 31 '23 at 06:18
64

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
Shawrup
  • 2,478
  • 2
  • 12
  • 21
Sma Ma
  • 3,343
  • 2
  • 31
  • 39
  • You mean spring / spring-boot ? (Spring 1x and Spring 2x) ! – smilyface Jul 10 '19 at 09:11
  • 1
    @smilyface spring boot. spring boot is also available in different versions: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot – Sma Ma Jul 11 '19 at 06:52
  • 4
    I use spring-boot 2.1.3, and `-Dspring-boot.run.profiles=local` did not work, `-Dspring.profiles.active=local` worked. – Donghua Liu Sep 02 '19 at 08:35
  • spring-boot 2.2.0: work both: `-Dspring-boot.run.profiles` and `-Dspring.profiles.active` – Grigory Kislin Apr 09 '20 at 19:13
  • 2
    Docs for this answer: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#run-example-active-profiles – Dawngerpony Jan 22 '21 at 17:28
34
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config

is not correct.

should be:

--spring.profiles.active=staging --spring.config.location=C:\Config
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Michael Yin
  • 393
  • 3
  • 2
  • 3
    This causes error "Unrecognized option: --spring.config.location" – James Watkins Apr 01 '16 at 18:34
  • 2
    -D is the correct way to set Java System properties. --something is a bash parameter. – Xdg Apr 06 '16 at 18:27
  • --spring.profiles.active work for me, same thing I referred from http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Pushkar May 18 '16 at 14:12
  • This also works for me when using Run As -> Java Application in Eclipse – Olivier Boissé Jan 14 '17 at 18:35
  • 21
    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` – Dexter Legaspi Jan 31 '19 at 20:22
29

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

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Rothin Sen
  • 444
  • 4
  • 8
  • 1
    Yes, and this avoids the need to muck around with the way Gradle hands system properties through to the application. – ben3000 May 01 '18 at 05:07
  • 1
    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. – Igor Donin May 15 '18 at 14:10
18

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.

Vinod Mohanan
  • 3,729
  • 2
  • 17
  • 25
  • 7
    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. – edufinn Apr 09 '16 at 12:40
  • 1
    this seems to be a gradle only solution, is there no genric solution ? – user1767316 Feb 07 '17 at 08:39
  • 2
    Where exactly are you adding this? Anywhere in the build.gradle file or in a specific location within the file? – Scala Enthusiast Jun 13 '18 at 23:04
  • @user1767316 you can set a environment variable with SPRING_PROFILES_ACTIVE=profile, works like a charm – vinicius gati Jan 14 '21 at 14:45
10

you can use the following command line:

java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
Rollen Holt
  • 497
  • 2
  • 11
  • 19
8

When setting the profile via the Maven plugin you must do it via run.jvmArguments

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"

With debug option:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"

I've seen this trip a lot of people up..hope it helps

JARC
  • 5,288
  • 8
  • 38
  • 43
  • 2
    has changed to `mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar`, see: https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html – rwenz3l Feb 23 '19 at 23:54
  • @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"` – John Smith Nov 14 '19 at 01:57
7

I think your problem is likely related to your spring.config.location not ending the path with "/".

Quote the docs

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

jst
  • 1,697
  • 1
  • 12
  • 13
  • 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. – Vinod Mohanan Jun 25 '15 at 13:18
  • 1
    You should follow the advice given in this question to pass the args to bootRun http://stackoverflow.com/questions/25079244/how-to-pass-jvm-options-from-bootrun – jst Jun 25 '15 at 14:37
  • Thank you. That really helped. – Vinod Mohanan Jun 25 '15 at 18:13
5

Michael Yin's answer is correct but a better explanation seems to be required!

A lot of you mentioned that -D is the correct way to specify JVM parameters and you are absolutely right. But Michael is also right as mentioned in Spring Boot Profiles documentation.

What is not clear in the documentation, is what kind of parameter it is: --spring.profiles.active is a not a standard JVM parameter so if you want to use it in your IDE fill the correct fields (i.e. program arguments)

ben3000
  • 4,629
  • 6
  • 24
  • 43
Eric Taix
  • 911
  • 1
  • 12
  • 24
5

This will fix your problem on windows :

mvn spring-boot:run -D"spring-boot.run.profiles"=env
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Vivek Singh
  • 93
  • 1
  • 5
4

For me helped to add "/" at the end of profiles location.

java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prod
user3650655
  • 361
  • 3
  • 8
1

If you use Gradle:

-Pspring.profiles.active=local
FredFlinstone
  • 896
  • 11
  • 16
1

We want to automatically pick property file based upon mentioned the profile name in spring.profiles.active and the path in -Dspring.config.location

application-dev.properties

If we are running jar in Unix OS then we have to use / at the end of -Dspring.config.location otherwise it will give below error.

Error :: java.lang.IllegalStateException: File extension of config file location 'file:/home/xyz/projectName/cfg' is not known to any PropertySourceLoader. If the location is meant to reference a directory, it must end in '/'

Example

java -Dspring.profiles.active=dev -Dspring.config.location=/home/xyz/projectName/cfg/ -jar /home/xyz/project/abc.jar

or

java -jar /home/xyz/project/abc.jar --spring.profiles.active=dev --spring.config.location=/home/xyz/projectName/cfg/
JoSSte
  • 2,953
  • 6
  • 34
  • 54
Suneet Khurana
  • 431
  • 5
  • 10
1

A way that i do this on intellij is setting an environment variable on the command like so:

test setup on intellij

In this case i am setting the profile to test

vinicius gati
  • 421
  • 5
  • 16
0

I was facing similar issues to run tests with different profiles in command line in springboot. I fixed that by first setting the profile and then running the test command like below :

Step 1 : export SPRING_PROFILES_ACTIVE=test(for mac/linux) or SET SPRING_PROFILES_ACTIVE=test(for windows)

Step2 : ./gradlew test --tests "com.maersk.snd.integrationtest.IntegrationTestPOC"

Above commands can be clubbed together like below :

export SPRING_PROFILES_ACTIVE=test && ./gradlew test --tests "com.maersk.snd.integrationtest.IntegrationTestPOC"

O_K
  • 922
  • 9
  • 14
0

Just as an addon, if you have a property mentioned in your application.properties file and you need to override that property from another config file you can use below property spring.config.additional-location (with -D since you pass from command line) We used to use this because we have one application.properties inside the jar and one external one in each of our servers' config folders, which is used to override any server specific properties.

Cozimetzer
  • 662
  • 4
  • 12