4

According to the SpringBoot documentation, the order of configuration is as:

Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)

Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)

Application properties outside of your packaged jar (application.properties and YAML variants).

Application properties packaged inside your jar (application.properties and YAML variants).

On my project I have a profile called "prod" and the following files:

  • application.yml (inside the jar)
  • application-prod.yml (inside the jar)

And I also want to override some of the properties using an external file. Since according to the docs, an external application.yml will be overridden by the internal application-prod.yml, I need to make sure that the external file is considered as a profile specific config file.

I have tried to use:

-Dspring.config.location=<my path>/application-prod.yml

and I have also tried:

-Dspring.config.location=file:<my path>/application-prod.yml

In all cases I get the value from the internal application-prod.yml

If I totally remove the internal config file then I get the value from the external (so I know that the config picks up the file).

I understand that this external file is considered as the equivalent to the generic application.yml and not a profile specific.

How can I configure it to be considered as a profile specific external config?

Community
  • 1
  • 1
Panos
  • 7,227
  • 13
  • 60
  • 95
  • Why not just name it `application-prod.yml`? Instead of trying to hack around it. – M. Deinum Jul 05 '18 at 10:26
  • I have named it `application-prod.yml` . It is just that spring's parameter `Dspring.config.location` ignores this is a profile specific config and considers it as a generic one. How do I specify this is a profile-specific? I'm trying to follow the order or rules as per the docs, not hack around it – Panos Jul 05 '18 at 10:44
  • You are overthinking it you don't need the `spring.config.location`. It will already override what you want. – M. Deinum Jul 05 '18 at 11:06
  • @M.Deinum Probably you haven't understood my question. I need the "spring.config.location" to pass the location of the external files – Panos Jul 05 '18 at 11:08
  • 1
    If it is in the same location as the jar you don't and if you want a location then specify a location NOT a full path to the file use only the directory (`file:/` spring boot will detect the `application.yml` etc) this is also clearly stated in the documentation. > *Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.* – M. Deinum Jul 05 '18 at 11:09
  • @M.Deinum Just saw your answer. Already solved it. I was setting the directory path in the parameter but it was not ending in `/` . It should be written a bit more clearly in the docs or at least have an example... – Panos Jul 05 '18 at 13:17

1 Answers1

3

Found the answer:

You need to use a Directory externally to set the profile specific configuration files, not using the file directly and it needs to end in /. So it has to be:

-Dspring.profiles.active=prod

-Dspring.config.location=/<some-path>/config/ (any path that ends in /)

and in there have a :

application-prod.yml

Panos
  • 7,227
  • 13
  • 60
  • 95