Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make spring.profile.include to include YAML files from subfolder?

I currently have 4 YAML files

application.yaml
application-local.yaml
application-test.yaml
application-dev.yaml

I assume that application.yaml is acting as default.

In the other 3 files contains

spring:
  profiles:
    include: default

My problem is that my application.yaml is very large and it contains properties that are hierarchical.

For example,

connection:
  baseUrl: 'http://localhost:5000'
  brokerUrl: 'http://localhost:8082'
  ...

workspaces:
  a:
    width: 45
    height: 100
    ...
  b:
    width: 56
    height: 125
    ...

I would like to keep the content in current application.yaml in multiple files in subfolders.

Let's say connection.yaml will contain

connection:
  baseUrl: 'http://localhost:5000'
  brokerUrl: 'http://localhost:8082'
  ...

workspace-a.yaml will contain

workspaces:
  a:
    width: 45
    height: 100
    ...

and lastly, workspace-b.yaml will contain

workspaces:
  a:
    width: 56
    height: 125
    ...

at the end the structure will be like

/resources
  application.yaml
  application-local.yaml
  application-test.yaml
  application-dev.yaml
  connection.yaml
  /workspaces
    a.yaml
    b.yaml

I've tried to put the following code in application.yaml but it didn't work.

spring:
  profiles:
    active: 'local'
    include:
      - connection.yaml
      - workspaces/a.yaml
      - workspaces/b.yaml

I also have tried different formats like

- classpath:/workspaces/a.yaml
- ./workspaces/a.yaml
- file:./workspaces/a.yaml

Please help :'(

like image 210
clumsy.kitten Avatar asked Mar 25 '26 14:03

clumsy.kitten


1 Answers

Since Spring-Boot 2.4 you can use spring.config.import to import into your config.

So you could use something like this in your application.yml

spring.config.import: connection.yml

Spring Blog post here

like image 104
athom Avatar answered Mar 27 '26 04:03

athom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!