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.
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
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.
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
is not correct.
should be:
--spring.profiles.active=staging --spring.config.location=C:\Config
you can use the following command line:
java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
There's another way by setting the OS variable, SPRING_PROFILES_ACTIVE.
for eg :
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
Reference : How to set active Spring profiles
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
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
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)