Distribute config files outside jar and accessible to application

I have an application with the following structure:

application
  -> src/main
      -> java
      -> resources (config files)

Currently, the code access the config files from the classpath and when running locally is all fine.

However, we’d like to deploy a jar without the config files in it, so that we can re-run the application with different configuration parameters without having to recreate the jar.

We’re using the application plugin for this and managed to get the config files into its own directory when distributing with ./gradlew distZip using the following configuration:

applicationDistribution.from('src/main/resources') {
    into 'conf'
}

The structure of the distribution ends up like:

distributions
    -> application
        -> bin/application (start script)
        -> lib/application.jar
        -> conf/** (config files)

Which is what I’m looking for, but I also need to:

  1. Make the files in the conf directory accessible from the code (via classpath?).
  2. Also, make them higher priority than the ones included in the jar or remove them from the jar for distribution (we still want to be able to run it locally for dev purposes).

I’m not sure if the config files should live in the resources directory. For day-to-day development is quite handy but it’s now becoming a problem for deployment given that they’re automatically included in the jar.

Being able to reconfigure an application without having to re-generate its jar is a reasonable requirement but haven’t found a way of properly implementing it.

@lmirabal did you find any solution to this requirement? Response is greatly appreciated.

When I need something like that, I usually modify the generated start scripts, so that they inject the APP_HOME value as system property to the execution and then use that path to read the files in the code, falling back to the ones in the classpath or even recreating the separate files from the one in the classpath.

@Vampire Thank you for the response, I achieved this by doing something similar. As I have application built using micronaut framework I just had to set the config system property value to the path of external config file in startup script and it’s done.

1 Like