javaspring-bootyamlflywaytest-data

Combine list from multiple spring-boot YAML files


Is it possible to combine lists of the same elements from multiple spring-boot configuration files written in YAML?

Example:

application-postgres.yml contains my postgres db informations. It also contains the flyway location of migration scripts:

spring:
  flyway:
    locations:
      - flyway/migrations/postgres

application-oracle.yml - the same with oracle migrations:

spring:
  flyway:
    locations:
      - flyway/migrations/oracle

Now if I want to start my application with test-data, I want to include test_data directory in addition to oracle or postgres migrations.
application-testdata.yml:

spring:
  flyway:
    locations:
      - flyway/test_data

When I run the application with active profiles postgres,testdata the postgres migrations are not loaded, since the locations entry is overridden by the testdata.

I know that I could create 4 config-files to run each db with and without the test-data.
But I want to combine migrations from multiple config files in order to not copy-paste configs, also new database could be introduced.


Update (Aug 2025):
flyway.location changed to spring.flyway.locations.
In Spring Boot 1.x, flyway migration was configured using flyway.location.
In Spring Boot 2 and 3, the property is changed to spring.flyway.locations.
Since Spring Boot 1.x is no longer supported, it's more appropriate to provide details relevant to currently supported versions.


Solution

  • Unfortunately, as described in the Spring docs, this isn't currently possible. If you know the set of profile names ahead of time, you could probably work around this by creating separate lists, prefixed with the profile name, and then in your bean, have in it code that merges them into the final list. That code would just iterate over all known profile name prefixes, and if a profile was not active, that list would be empty.

    Unfortunately, this seems harder if you don't own the bean, such as in your Flyway case. It might still be possible though.