Apache Commons Configuration File Location Strategies

585 views Asked by At

I'm new to Linux, Java, and Apache Commons Configuration and am having issues locating a properties file between environments.

I'm building a Spring API with Maven on Windows and have a .properties file i want to pull settings from. I also am trying to publish this API to a Linux based Docker container and am confused about the file location strategies.

Everything works on Windows with the properties file in C:\users\myusers\app.properties. On the Linux container, i have both my jar and properties file here /usr/bin/myapp but I am unsure how to use any of the strategies to access both, as this fails in Docker/Linux.

Is there a best practice on where i should store the properties file in the container?

CombinedLocationStrategy strategy = new CombinedLocationStrategy(
    Arrays.asList(new HomeDirectoryLocationStrategy(), new FileSystemLocationStrategy()));

Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder = 
       new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                           .configure(params.fileBased()
                                   .setLocationStrategy(strategy)
                                   .setFileName("app.properties"));
Configuration propConfig = builder.getConfiguration();

rootDirectory = propConfig.getString("rootfolder");
1

There are 1 answers

1
aksappy On

Just like in linux, we can mount volumes in docker as well. This will help in having a single volume for shared properties or locations.

Firstly, mount a directory from the host system as a volume into the container. Please see the documentation here

If you mount it say - /opt/myapp , then the files will be visible for the container at /opt/myapp.