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.
bootRun
command line also
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
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
-Dspring-boot.run.profiles=local
did not work, -Dspring.profiles.active=local
worked.
-Dspring-boot.run.profiles
and -Dspring.profiles.active
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
is not correct.
should be:
--spring.profiles.active=staging --spring.config.location=C:\Config
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
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
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.
bootRun { systemProperties = System.properties }
. This command will copy all parameters passed with -D
switch with the same keys to systemProperty
map.
you can use the following command line:
java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
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
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
, see: docs.spring.io/spring-boot/docs/current/maven-plugin/examples/…
springmvn="mvn clean spring-boot:run -Dspring.profiles.active=local -Dspring-boot.run.profiles=local"
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).
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)
For me helped to add "/" at the end of profiles location.
java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prod
If you use Gradle:
-Pspring.profiles.active=local
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/
A way that i do this on intellij is setting an environment variable on the command like so:
https://i.stack.imgur.com/EiL0d.png
In this case i am setting the profile to test
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"
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.
Success story sharing
SPRING_PROFILES_ACTIVE
exported via~/.bash_profile
.export SPRING_PROFILES_ACTIVE=e2e
java -Dspring.profiles.active=$ENV -Dspring.config.location=file:///aws-secrets-manager/properties/application-$ENV.properties /code/app.jar