which application.properties file will be picked in spring boot

2k views Asked by At

In my spring boot project, I have application-default.properties file in resource folder and another application.properties file in a folder named "config". This folder I have manually created inside the project folder. Now when I run the spring boot application via eclipse it picks up properties file inside config directory and when I create jar of the same project and run via java -jar, then it picks up the application.properties file in resource folder. Can someone explain this behavior as why while running form eclipse, the properties file in config folder overrides the one in resource folder ?

1

There are 1 answers

0
Ananthapadmanabhan On

By default , in springboot the precedence order for the application.properties is as follows :

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

Since the application.properties that you created in the config folder is not under the resource directory it might not be included in the jar(check the jar to confirm if it is present). When maven creates the jar anything under the resource dir will be copied over to the jar, so if you want the config folder to be present inside the jar then move it to under the resource dir. Or if you want to keep the conf external then, Create the config folder in same dir as the jar or set a classpath to the config to take it up while deploying using java -jar. You could specify external location for the property file like :

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

Read official doc.