Using external third-party properties file with Spring Boot Fat Jar

983 Views Asked by At

I have a Spring Boot app that will be deployed as a fat jar. It integrates with Atlassian's Crowd for authentication. This requires a crowd.properties file to be available on the classpath. I would prefer to not bundle the properties file (which includes a password) with the jar.

Is there a way to tell Spring Boot to include another file or directory for where to search for property files?

When doing this with a standalone/external Tomcat in the past, I would use the shared.loader property in catalina.properties to specify a directory where additional property files would be available.

I've tried including the file in the root location of the jar, as well as a /config location, but to no avail. Atlassian also has a page that indicates using a -Dcrowd.properties=... command line parameter to set this, but also to no avail.

Note: This is not referencing application.properties in external locations.

1

There are 1 best solutions below

0
On BEST ANSWER

Okay, so I was able to figure this out with the help of this answer, specifically the section under "Original answer".

It turns out that a two part solution was required.

  1. The spring-boot-maven-plugin needed to have some configuration set. By setting the layout to ZIP, it will use the PropertiesLauncher rather than the JarLauncher, which (I believe) allows you to use the loader properties.

`

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
    </configuration>
</plugin>
  1. Using the -Dloader.path=/folder/with/property/file/ command line parameter to provide the location of the properties file. Note: this must come after the -jar parameter.

For more information on the why, read on.

Based on the Spring documentation for "Launching Executable Jars", there are three launchers to load the files for an application. By default the JarLauncher is used, which limits the locations you can retrieve resources. The PropertiesLauncher on the other hand will look in BOOT-INF/lib/ but also in loader.path, which you can provide additional folders.