Add properties file to generate distribution with "application" plugin

I’m using the application plugin to generate a zip archive with a convenient launcher script so the user does not have to do much other than extract the zip and run the tool.

I would like to make it even easier by including a ready to go properties file in the generated Zip distribution and also have it added to the classpath in the launcher script. That way the user can just run the app with minimal command line args.

How can I go about :

  1. Getting the properties file added to the resultant Zip/Tar package
  2. Have that properties file added to the classpath used by the wrapper script

Many thanks,

Fred.

You’ll need to 1) configure the ‘main’ distribution to include the properties file and 2) configure the ‘startScripts’ task to add the file to the classpath.

apply plugin: 'application'

mainClassName = 'Main'

distributions {
    main {
        contents {
            from('src/conf/foo.properties') {
                into 'lib'
            }
        }
    }
}

tasks.withType(CreateStartScripts) {
    classpath += files('src/conf/foo.properties')
}

Thanks! This did the trick.

As I don’t think the shown approach will work (at least didn’t work for me) as you cannot add files to the class path, here four other approaches.


For as less config as possible

Put the config file into src/dist/lib/config and use

startScripts {
    classpath += files('config')
}

If the file must not be moved in the source

Configure the main distribution

distributions {
    main {
        contents {
            from('src/conf/foo.properties') {
                into 'lib/config/'
            }
        }
    }
}

startScripts {
    classpath += files('config')
}

Another approach with a little more sane directories

Put the config file into src/dist/config or configure the main distribution accordingly and use

startScripts {
    classpath += files('config')
    doLast {
        unixScript.text = unixScript.text.replace('$APP_HOME/lib/config', '$APP_HOME/config')
        windowsScript.text = windowsScript.text.replace(/%APP_HOME%\lib\config/, /%APP_HOME%\config/)
    }
}

An approach reading the file directly instead of using class path lookup

Put the config file into src/dist/config or configure the main distribution accordingly and use

startScripts {
    doLast {
        def mainClass = application.mainClass.get()
        unixScript.text = unixScript.text.replace(mainClass, $/$mainClass "$$APP_HOME/config/foo.properties"/$)
        windowsScript.text = windowsScript.text.replace(mainClass, /$mainClass "%APP_HOME%\config\foo.properties"/)
    }
}

And then in the code use new File(args[0]) or similar to read it.

Attention: Do not just omit the startScripts configuration and use new File("config/foo.properties") as that will be relative to the current working directory of the user starting the application and will thus be very fragile.