Spring Boot - Loading application property files from Current Directory

[Updated: Jul 13, 2017, Created: Jul 12, 2017]

In this example, we will learn how to load Spring Boot application property files from 'current directory' or '/config subdirectory of the current directory' (as stated in spring boot ref doc).

Here 'current directory' refers to the location where we execute the application. In this example, we will run the application via spring boot maven plugin. We will also run the executable application jar. Running from an IDE does not work, because an IDE cannot copy (or load) the properties files to the location where we intend to execute our jar.

Writing a simple Boot application

@SpringBootConfiguration
public class ExampleMain {
    @Bean
    MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication bootApp = new SpringApplication(ExampleMain.class);
        bootApp.setBannerMode(Banner.Mode.OFF);
        bootApp.setLogStartupInfo(false);
        ConfigurableApplicationContext context = bootApp.run(args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.startApplication();
    }

    private static class MyBean {

        @Value("${app.title}")
        private String appTitle;

        public void startApplication() {
            System.out.printf("-- in MyBean app title: %s --%n", appTitle);
        }
    }
}

We are going to put following property file in the maven root location (where the pom.xml file is).

application.properties

app.title=My Spring Application

Running application via Boot maven plugin

Running the exploded application via Boot maven plugin:

mvn -q spring-boot:run

Output

D:\example-projects\spring-boot\boot-current-directory-prop-file>mvn -q spring-boot:run
-- in MyBean app title: My Spring Application --

Running executable jar

Packaging the application

mvn -q clean package spring-boot:repackage

Output

D:\example-projects\spring-boot\boot-current-directory-prop-file>mvn -q clean package spring-boot:repackage

Placing the property file at 'current directory' location

We need to place the application.properties where we are going to run the executable jar. So we are going to place it just beside the executable jar (in the maven target directory):

copy application.properties target\application.properties

Output

D:\example-projects\spring-boot\boot-current-directory-prop-file>copy application.properties target\application.properties
1 file(s) copied.

Executing the jar

java -jar boot-current-directory-prop-file-1.0-SNAPSHOT.jar
D:\example-projects\spring-boot\boot-current-directory-prop-file>cd target

D:\example-projects\spring-boot\boot-current-directory-prop-file\target>dir
 Volume in drive D is Data
 Volume Serial Number is 68F9-EDFA

 Directory of D:\example-projects\spring-boot\boot-current-directory-prop-file\target

07/12/2017  10:48 PM    <DIR>          .
07/12/2017  10:48 PM    <DIR>          ..
07/06/2017  07:26 PM                31 application.properties
07/12/2017  10:41 PM         6,651,337 boot-current-directory-prop-file-1.0-SNAPSHOT.jar
07/12/2017  10:41 PM             4,428 boot-current-directory-prop-file-1.0-SNAPSHOT.jar.original
07/12/2017  10:41 PM    <DIR>          classes
07/12/2017  10:41 PM    <DIR>          generated-sources
07/12/2017  10:41 PM    <DIR>          maven-archiver
07/12/2017  10:41 PM    <DIR>          maven-status
               3 File(s)      6,655,796 bytes
               6 Dir(s)  60,773,568,512 bytes free

D:\example-projects\spring-boot\boot-current-directory-prop-file\target>java -jar boot-current-directory-prop-file-1.0-SNAPSHOT.jar
-- in MyBean app title: My Spring Application --

D:\example-projects\spring-boot\boot-current-directory-prop-file\target>

This should also work if we place the application.properties file in config/ directory in both cases.

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.5.4.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.9.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Boot Setting Active Profile Select All Download
  • boot-current-directory-prop-file
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also