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
I am not sure if you are dealing with the same situation than me, but in my case I have a jar and a *.properties file outside of it. What I did to get the *.properties file located outside the jar was the next:
@Configuration
public class ApplicationContext {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("application.properties"));
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
When I am setting the location of the application.properties file I created FileSystemResource object, which allow me get the properties.files which is located next to the jar. If your .properties files are in classpath, for example, you can use other classes (like ClassPathResource). You can read other classes that spring offers to get a Resource object under the package org.springframework.core.io. .
I hope this comments helps.