lospejos lospejos - 1 year ago 329
Java Question

Gradle build file with conf folder with properties not in jar but on classpath

Could you please share a build.gradle script example with configured folder "conf":

  1. This folder including all containing files is packaged into zip file during "gradle distZip";

  2. Files in this folder are on classpath (e.g. accessible to ClassLoader) in startscripts files (in bin folder);

  3. These files also are on classpath when running gradle run, gradle build etc.

Addition #1:

I've done actions mentioned in answer pointed by @wakjah, files was copied to a zip file and was not put into jar during
gradle distZip
, this covers my requirement #1. But this folder is not on classpath in start scripts from
folder (requirement #2 is not covered). Files are not on classpath when running
gradle clean run
(requirement #3 is not covered).


Answer Source

As mentioned here Gradle distZip config files you can just add the folder dist/config to your src folder and it will be included by the distZip task automatically.

As for adding it to the runtime classpath, just add the folder as a dependency (see below).

Requirement #2 is a little harder, due to limitations with startScripts. This question has an answer that proposes a workaround, however: Adding classpath entries using Gradle's Application plugin

The following code collects all of the above:

// put config files in src/dist/config

dependencies {
  runtime files('src/dist/config')

startScripts {
  classpath += files('src/dist/XxxAPlaceHolderForAConfigxxX')
  doLast {
    def windowsScriptFile = file getWindowsScript()
    def unixScriptFile    = file getUnixScript()
    windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\XxxAPlaceHolderForAConfigxxX', '%APP_HOME%\\config')
    unixScriptFile.text  = unixScriptFile.text.replace('$APP_HOME/lib/XxxAPlaceHolderForAConfigxxX', '$APP_HOME/config')

I have tested this and it appears to work. The correct classpath entries appear in both the run scripts and the command line used by gradle during the run task.

EDIT: In your comment you implied that you also want this to be available in the classpath of your build script. If that is the case, you need to add a buildscript dependency like this:

buildscript {
  dependencies {
    classpath files('src/dist/config')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download