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

后端 未结 1 1824
醉话见心
醉话见心 2021-02-06 15:27

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 \
1条回答
  •  花落未央
    2021-02-06 16:28

    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')
      }
    }
    

    EDIT #2: In another comment you mention that adding the runtime dependency has the unintended consequence of producing an extra, invalid, entry in the start script classpath. You could fix this by just adding to the classpath of any JavaExec tasks, rather than using a dependency. So replace the dependencies { runtime { ... } } block with

    tasks.withType(JavaExec) {
      classpath += files('src/dist/config')
    }
    

    0 讨论(0)
提交回复
热议问题