How to set active profile and config location from command line in spring boot
In this tutorial we are going to learn about setting the active profile and config location from command line in a spring boot application.
The 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=localSpring-Boot 1.x
-Dspring.profiles.active=localexample usage with maven:
Spring-Boot 2.x
mvn spring-boot:run -Dspring-boot.run.profiles=localSpring-Boot 1.x and 2.x
mvn spring-boot:run -Dspring.profiles.active=localMake 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,barOR
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.jarOption 2: Program arguments
java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config