6

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?

unixsmurf
  • 5,852
  • 1
  • 33
  • 40
dondragon2
  • 535
  • 5
  • 15

2 Answers2

8

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.

Community
  • 1
  • 1
Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • 2
    When you use "-jar" with the Java command there is no "-cp". Another approach is to use a specialized classloader (Spring Boot has such a feature - look up `PropertiesLauncher` in the user guide). – Dave Syer Aug 17 '14 at 10:27
  • Thanks, I have missed the way he is launching it! Added a comment to my response. – Jan Zyka Aug 17 '14 at 10:51
2

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:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

btiernay
  • 7,873
  • 5
  • 42
  • 48
  • thanks @btiernay. I saw that in the reference also. Is it the same for i18n properties files? I don't remember seeing anything like that. – dondragon2 Nov 14 '14 at 00:59
  • @dondragon2 Not really, but you may be interested in http://justinrodenbostel.com/2014/05/13/part-4-internationalization-in-spring-boot/ – btiernay Nov 15 '14 at 00:46