Reputation: 595
I am trying to make a Spring Boot application. Everything is fine once I deploy to the fat jar file with everything contained in it. But, what I actually want is the configuration files to be located externally. for example I have the following directory structure:
bin - contains startup and shutdown scripts
conf - all configurations. i.e. application.properties, logback.xml i18n.properties
logs - log files
libs - app.jar
If I use this directory structure and execute the jar using
java -cp ./conf -jar ../libs/app.jar
then the properties in the conf directory are not loaded or recognized. Is there a better way to do this maintaining the directory structure above? Or, what is the alternative/best practice?
Upvotes: 6
Views: 6977
Reputation: 17898
Boot external config is what you are looking for.
Especially it mentions:
SpringApplication will load properties from
application.properties
files in the following locations and add them to the Spring Environment:
- A /config subdir of the current directory.
- The current directory
- A classpath /config package
- The classpath root
So I would say adding the config folder on classpath is good step. Them it should find application.properties
and load it automatically.
For different config files I use:
@Configuration
@EnableAutoConfiguration
@PropertySource({
"classpath:path/some.properties",
"classpath:another/path/xmlProperties.xml"
})
public class MyConfiguration {
// ...
}
Edit: As Dave pointed out (Thank Dave!) there is either -cp or -jar, so you can't add it to classpath like that. But there are options. This should help you to solve the problem: Call "java -jar MyFile.jar" with additional classpath option.
Additionally @PropertySource
doesn't require the resources to be classpath resources if I'm not mistaken.
Upvotes: 8
Reputation: 8129
It should also be mentioned that there is a spring.config.location
parameter that allows one to specify a file system / classpath location for externalized configuration files. This is documented in the following section of the Spring Boot reference guide:
Upvotes: 2