3

In my application I have these properties files:

application.properties
application-prod.properties

Inside I have the same property

spring.datasource.password=my-dev-password #for the default one
spring.datasource.password=${PROD_DATABASE_PASSWORD} #for the prod file

On the server I run my application like :

java -jar "myjar.jar" --spring.profiles.active=prod

Everything works fine so far.

Now I want to use an extra file to override the same property on the server like :

java -jar myjar.jar --spring.profiles.active=prod --spring.config.additional-location=file:/to/folder/application.properties

but it didn't work !

I've tried to pass it as a java property but it didn't work neither !

java -Dspring.config.additional-location=file:/to/folder/application.properties -jar myjar.jar --spring.profiles.active=prod

What I have missed here ?


@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

I'm using spring-boot 2.3.5


UPDATE

When I reference only the folder it works :

--spring.config.additional-location=file:/to/folder/

I thought that it takes only folder in contrary to spring.config.location but when I've looked to the code both are loaded with the same code in ConfigFileApplicationListener :

private Set<String> getSearchLocations() {
    Set<String> locations = getSearchLocations(CONFIG_ADDITIONAL_LOCATION_PROPERTY);
    if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {
        locations.addAll(getSearchLocations(CONFIG_LOCATION_PROPERTY));
    }
    else {
        locations.addAll(
                asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS));
    }
    return locations;
}
1615903
  • 32,635
  • 12
  • 70
  • 99
flywell
  • 384
  • 3
  • 20
  • When you use the updated config --spring.config.additional-location=file:/to/folder/. I believe it still loads your props from default one and not from the file present in additional-location? – TriS Jul 29 '21 at 20:21
  • yes `spring.config.additional-location` overrides those in the jar `application.properties` – flywell Jul 30 '21 at 07:52
  • I played around for some time with my Spring Boot 2.3.4 application. It is working for me. Something you could try ro debug. Rename the file in spring.config.additional-location to other.properties (or something different from Spring defaults) and try if this has an impact. Double check, if you are using proper path to the file. – Elmar Brauch Jul 30 '21 at 19:02

2 Answers2

0

Profile Specific Properties take precedence over default/additional properties. For reference spring-boot external config.

You can also see the same in Code ConfigFileApplicationListener.java

-1

Try this.

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

In more details refer this doc.

aristotll
  • 8,694
  • 6
  • 33
  • 53
Thunderr
  • 15
  • 4