3

I'm working on a project and have a use case where I need to provide application.properties file for Spring Boot from outside the JAR.

According to Baeldung, the priority order for picking up the application.properties is

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The issue with first two is I'll need to navigate to the directory containg the configs to run the JAR. It sounds no issue when working on local but wont be a feasible solution when deploying on remote hosts as through CI/CD frameworks.

I'm trying to find a mechanism using classpaths and avoid using spring boot's command line options mentioned over here or setting up environment variables.

I'm unable to figure out how to setup classpath while running FAT JAR and specify configs all together. If you can, please help me figure it out!

Thanks in advance :)

EDIT : I understand there are ways to achieve this using Spring Boot's command line options such as spring.config or loader.path etc.

I was trying to find a more implicit solution based on classpath and directory structures only to make it less coupled with the fact that Spring Boot is being used.

  • Have you tried to use `java -jar app.jar --spring.config.location=another-location.properties` – Ehab Qadah May 20 '20 at 22:05
  • Hey I wish to use classpaths directly without having to use Spring Boot run time options provided such as spring.config, loader.config etc Just JAR, properties file and classpath specfifying the required locations to JAR and configs. (Last 2 of the priority options) – Mridul Gupta May 21 '20 at 07:38

2 Answers2

2

According to the Spring docs, you can define external config locations using the spring.config.location property. More specifically:

If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.

Config locations are searched in reverse order. By default, the configured locations are:

classpath:/,classpath:/config/,file:./,file:./config/.

The resulting search order is the following:

file:./config/ file:./ classpath:/config/ classpath:/

When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value

classpath:/custom-config/,file:./custom-config/ the search order becomes the following:

file:./custom-config/ classpath:custom-config/

Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:

file:./custom-config/ classpath:custom-config/ file:./config/ file:./ classpath:/config/ classpath:/

An example usage for a directory containing your external configi would look like:

java -jar myproject.jar --spring.config.location=file:/custom-config-dir/

Or directly to an external config file:

java -jar myproject.jar --spring.config.location=file:/custom-config-dir/custom-config.properties
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • Hey I appreciate the answer but we're trying to achieve it without using spring provided run time options. I have added an EDIT to question. Do you know a way coherent with that? – Mridul Gupta May 21 '20 at 07:43
0

Specify custom config location as VM argument is another option.

java -Dspring.config.location=<config-dir-path> -jar demo.jar
Kumar V
  • 1,570
  • 1
  • 12
  • 19
  • More options on this thread. https://stackoverflow.com/questions/39716796/spring-boot-executable-jar-with-classpath – Kumar V May 20 '20 at 22:38
  • Thanks for the answer but we're trying to achieve it without using spring provided run time options. I have added an EDIT to question. Do you know a way coherent with that? – Mridul Gupta May 21 '20 at 07:44
  • what do you meant by spring provided runtime options . Do you mean like uing it through a rest service kind of stuff . You Want to Change the Properties in Runtime while your app is running ? – Sanjay May 21 '20 at 08:02
  • i mean spring boot run time options to set spring.config , loader.path etc I want to use classpath and jar only – Mridul Gupta May 21 '20 at 08:47