Spring external .properties file

Before we proceed, be sure the check out the spring-cloud-config. Depending on your use case, this might be what you want , Spring Cloud Config.

Lets say that we have following .properties files in our project:

  • application.properties
  • application-dev.properties
  • application-prod.properties
  • custom.properties

Spring will use these files, if we do not tell it otherwise. We can override the location of these files by using spring.config.location environment property.

NB! One thing to note though. If you just want to override the application.properties file (or profile properties files) you can just put it in the same directory where your jar file is! Spring boot will pick it up and use it over the one inside the jar. This does not seem to work with custom file names though, so let’s see how to specify file locations.

Setting a custom file location for custom.properties file:

--spring.config.location=classpath:/,C:/Users/Dimitar/Desktop/custom.properties

More about it, you can read here -> 24.3 Application Property Files.

Running the app from the .jar file:

java -jar properties-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/,C:/Users/Dimitar/Desktop/custom.properties

Running the app with Maven (this applies to spring boot 2):

If you know of a better way to pass the arguments, let me know! (Spring Boot Maven Plugin docs)

mvn -Dspring-boot.run.jvmArguments="-Dspring.config.location=C:/Users/Dimitar/Desktop/custom.properties,classpath:/" spring-boot:run

Running the app from IntelliJ:

Run -> Edit Configurations -> Program arguments input

Sample code is on Github.