Java Dailly Tip: Setting active profile and config location from command line in spring boot
Problem
I wrote an application in spring. The application has three profiles to run, so I have three files:
- application-development.yml
- application-staging.yml
- application-production.yml
The files are located in the / src / main / resources directory. I am using the Gradle plugin for the eclipse. When I try to "bootRun" I set the command line arguments in my eclipse gradle configuration as:
-Dspring.profiles.active = staging -Dspring.config.location = src / main / resources / application-production.yml
However, the command line property is not mirrored and my active profile is always set as developer (which I mentioned in application.yml). What am I missing?
Solution
My best practice is to define this as a VM "-D" argument:
-Dspring-boot.run.profiles=local mvn spring-boot:run -Dspring-boot.run.profiles=local
Make sure to separate them with a comma for multiple profiles:
mvn spring-boot:run -Dspring.profiles.active=local,foo,bar
There's another way by setting the OS environment variable, SPRING_PROFILES_ACTIVE.
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
For gradle:
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" }