flywell
flywell

Reputation: 356

Spring boot override prod properties from additional location file

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;
}

Upvotes: 3

Views: 2509

Answers (2)

Thunderr
Thunderr

Reputation: 15

Try this.

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

In more details refer this doc.

Upvotes: -1

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

Upvotes: 0

Related Questions