I have a spring boot application.
I have three profiles in my application-> development, staging and production. So I have 3 files
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
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)
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
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
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 '/'
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/