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

↘锁芯ラ 提交于 2019-12-08 12:51:01

问题


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:


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.



来源:https://stackoverflow.com/questions/51735728/using-external-third-party-properties-file-with-spring-boot-fat-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!