How to use a config directory with application plugin?

I have a Java application that needs to read config files from somewhere on the classpath, outside of the .jar files and lib directory. The config directory is shared between projects, so I create a symlink to the shared directory from build/install/app/config. My build file is something like this:

apply plugin: 'java'
apply plugin: 'application'

dependencies {
    runtime files("$rootDir/../config")
}

installDist << {
    def link = Paths.get("$buildDir/install/$applicationName/config")
    def target = Paths.get("$rootDir/../config").normalize()
    Files.createSymbolicLink link, target
}

startScripts {
    // http://stackoverflow.com/a/18712556/320036
    doLast {
        def unixScriptFile = file getUnixScript()
        unixScriptFile.text  = unixScriptFile.text.replaceFirst(
            /APP_HOME\/lib\/config(:|$)/,
            'APP_HOME/config$1')
    }
}

There are two problems:

  1. The resulting app/lib/ directory contains the config files, because of the runtime dependency. They are not on the classpath so they’re ignored, but how can I exclude them? I tried doing this:

    applicationDistribution.with {
        exclude('config/**')
    }
    

    But it doesn’t work. Indeed, even exclude('**') doesn’t seem to exclude anything.

  2. The distZip task doesn’t include the config directory. Although I want it to be a symlink when I build with installDist, I’d like it to be copied into the zip file when using distZip.

Maybe I’m just going about this in entirely the wrong way?

Full build files can be found here:

To answer my second question:

  1. Include a symlink from src/dist/config to the shared config directory. That means the config directory will be copied into the zip file and also into the install directory.

  2. Add a command in installDist to delete that directory before creating the new symlink.

    installDist << {
        delete "$buildDir/install/$applicationName/config"
        def link = Paths.get("$buildDir/install/$applicationName/config")
        def target = Paths.get("$rootDir/../config").normalize()
        Files.createSymbolicLink link, target
    }
    

I’m still interested in answers to the first question :slight_smile: