Spring Properties File Outside jar

1. Overview

Property files are a common method that we can use to store project-specific information. Ideally, we should keep it external to the packaging to be able to make changes to the configuration as and when required.

In this quick tutorial, we’ll look into various ways to load the properties file from a location outside the jar in a Spring Boot application.

2. Using the Default Location

By convention, Spring Boot looks for an externalized configuration file – [.crayon-pre .crayon-code]application.properties [crayon-5c73a186c8530009937282 .crayon-syntax .crayon-syntax-inline .crayon-theme-familiar .crayon-theme-familiar-inline .crayon-font-consolas][.crayon-pre .crayon-code][.crayon-v]or application.yml### in 4 predetermined locations in the following order of precedence:

  • A /config subdirectory of the current directory

  • The current directory

  • A classpath /config package

  • The classpath root

Therefore, a property defined in application.properties and placed in the /config subdirectory of the current directory will be loaded. This will also override properties in other locations in case of a collision.

3. Using the Command Line

If the above convention doesn’t work for us, we can also configure the location directly in the command line:

java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties

We can also pass a folder location where the application will search for the file:

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config

And, an alternative approach is running the Spring Boot application through the Maven plugin. There, we can use a -D parameter:

mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"

4. Using Environment Variables

Or, let’s say that we can’t alter the start-up command. What’s great is Spring Boot will also read the environment variables SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION:

export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar

Note that the default file will still be loaded. But in case of a property collision, the environment-specific property file takes precedence.

5. Programmatically

Or, if we want programmatic access, we can register a PropertySourcesPlaceholderConfigurer bean:

public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/Users/home/conf.properties"));
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

Here, we’ve used PropertySourcesPlaceholderConfigurer to load the properties from a custom location.

6. Excluding a File from the Fat Jar

The Maven Boot plugin will automatically include all files in the src/main/resources directory to the jar package.

If we don’t want a file to be part of the jar, then we can a simple configuration to exclude it:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/conf.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

In this example, we’ve filtered out the conf.properties file from being included in the resulting jar.

7. Conclusion

As we can see, the Spring Boot framework itself takes care of externalized configuration for us.

Often, we just have to place the property values in the correct files and locations, but we can also use Spring’s Java API for more control.

As always, the full source code of the examples is available over on GitHub.

Leave a Reply

Your email address will not be published.