Search code examples
spring-bootpropertiesexternalexecutable-jar

Spring Boot 2: Using external Property-Files


I have a executeable jar what I want to configure by properties outside of the jar. What works fine is application.properties, when putting it to config folder close to the jar. But I have a second property-file what seems not to be picked up and I would like to have the best practice for that.

The folder config looks like: enter image description here

In the config-folder you will find: enter image description here

Both property-files are also in the src/main/resources folder.

My StartClass looks like:

@SpringBootApplication
@PropertySource("migration-shrink.properties")
public class MigrationShrinkApplication implements CommandLineRunner {}

My bat file looks like:

java -jar migration-shrink-0.0.1-SNAPSHOT.jar -Dspring.config.location=./config/migration-shrink.properties

I wanted to separate Spring-Configuration from Application-Configuration, thats why I have two different property-files.

Thank you!


Solution

  • The @PropertySource annotation is not necessary.

    As of Spring Boot 2.0, you can declare additional locations with:

    -Dspring.config.additional-location=./config/migration-shrink.properties
    

    Keep in mind that those additional locations are searched before others, so values can be overridden in the other locations.

    See the Spring Boot reference documentation.