简体   繁体   中英

Read external .properties file from the install directory (not the current directory)

I am distributing a Spring Boot application as a zipped "bootJar" using the Gradle Application plugin and the "distZip" task. The end-user will get the zip file, unzip it, and run it by just typing "myApp" (a shell script nicely created by the plugin).

I would like the end-user to create a "myapp.properties" file (a name I chose) and put it in the installation directory, or a "config" directory under the installation directory.

Suppose I set up my embedded (in the jar) application.properties file as follows:

  • spring.config.import = file:./myapp.properties will only read from the current directory
  • spring.config.import = file:/etc/myapp.properties will read from the specified directory -- but I don't know what this is at build time (the end-user determines it at installation time )

How can I set up my application so that Spring Boot can read properties from an external file whose location is specified later?

NOTE: I know I can play around with the generated scripts to pass in environment variables or Spring Boot properties, but I was hoping to do this completely within Spring Boot so I don't need to modify the nicely generated shell scripts.

spring.config.import = file:./myapp.properties will only read from the current directory spring.config.import = file:/etc/myapp.properties will read from the specified directory -- but I don't know what this is at build time (the end-user determines it at installation time)

Why overcomplicate this.

Place inside the jar all the properties that you want to be statically configured as default values when you build the application.

Embedded application.properties

 server.port = 8080
 prop1.element = something

Then the client can create another file application.properties and place it in the same directory with the jar and define more properties which are not already defined.

 prop2.element = something2   
 prop3.element = something3

By default Spring Boot will load properties both from the embedded file as well from the file in the current directory where the jar is placed during startup.

In the external application.properties you can also overwrite properties existing in the embedded application.properties . So if the external file in the current directory same as the jar is the following

 prop2.element = something2   
 prop3.element = something3
 prop1.element = something4 <--- this value here will overwrite the value 'something' from embedded file 

According to spring doc

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  1. /config subdirectory of the current directory.

  2. The current directory

  3. classpath /config package

  4. The classpath root The list is ordered by

Precedence (properties defined in locations higher in the list override those defined in lower locations ).

After having more input from the comments, it seems that you face another issue as well. You start the application from command line from another directory so that is counted as the directory where spring will look for the external configuration instead of where the jar is placed.

So for example let's say that the jar is placed inside the target folder that exists in current directory. You start the application using the following command:

java -jar target/demo-0.0.1-SNAPSHOT.jar

But then the external application.properties existing inside target folder is not loaded from spring because you executed the command from another directory. This can be solved if you start the application in the following way

java -jar -Dspring.config.additional-location=./target/ target/demo-0.0.1-SNAPSHOT.jar

This should not be difficult as you already provide the path where the jar exists in the command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM