Setting active profile and config location from command line in spring boot

O-OF-N picture O-OF-N · Jun 25, 2015 · Viewed 293.5k times · Source

I have a spring boot application.

I have three profiles in my application-> development, staging and production. So I have 3 files

  1. application-development.yml
  2. application-staging.yml
  3. 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.

Answer

RenRen picture RenRen · May 25, 2016

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