Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Spring Application by merging yml files

Tags:

spring-boot

Is there a way in spring boot to merge properties from different config files and start the application?

Ex: My application-local.yml which is the one that gets used by default had following properties

server:
  port: 8080
spring:
  profiles: local

propertyA: xxx
propertyB: yyy

Now instead of having to copy all the properties from the local to lets say application-QA.yml like this

server:
  port: 8081
spring:
  profiles: local

propertyA: xxx
propertyB: zzz

where only port & propertyB has been updated, can I just have something like below?

application-QA.yml:

server:
  port: 8081
propertyB: zzz

At the end I want to have the following ability to start my applicatio

  • with local - ./gradlew bootrun should pick up properties from application-local.yml which is what is happening now
  • with QA - ./gradlew bootrun -Dsome.property=QA should merge properties from local and QA and start the application

Right now we have to copy port & propertyB to application-local.yml and start the application in order to point to QA environment and I would like to eliminate that.

Note: ./gradlew bootrun -Dspring.profiles.active=QA doesn't seem to work for me since I will need all the properties in application-local.yml to be in application-QA.yml and not just the properties I want to override.

like image 662
lakshmi Avatar asked Mar 09 '16 21:03

lakshmi


1 Answers

You can do this using spring profiles. You can activate many profiles together and every profile will read properties from an additional application-.yml

  • If you activate a profile 'local', spring searches for a file application-local.yml
  • If you activate a profile 'QA', spring searches for a file application-QA.yml

So you set the common properties in application-local.yml, and only the different ones e.g. the port in application-QA.yml .

You activate both profiles together with

-Dspring.profiles.active=local,QA

See also spring howto-properties-and-configuration

like image 100
Stefan Isele - prefabware.com Avatar answered Oct 13 '22 19:10

Stefan Isele - prefabware.com