Spring boot how to read properties file outside jar

I’m a bit confused by the title of the question and the description. Hopefully I won’t confuse you even more with my comments.

In general, Spring Boot is VERY opiniated about project structure as well as the binary created. The recomended way (Spring Boot opinion) is to build a jar with all dependencies inside (fat jar). If you need configuration properties defined outside your fat jar (or war if that’s what you built), Spring Boot offers many options (see reference 1). I like my apps to point to an external file using the flag (spring.config.location) which can be set with a system property:

java -jar -Dspring.config.location=<path-to-file> myBootProject.jar

Notice that you can do something similar by using an environment variable to define where your external file lives.

I hope this helps!

References:
1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Leave a Comment