Adding classpath entries using Gradle's Application plugin

后端 未结 5 503
攒了一身酷
攒了一身酷 2021-02-01 18:57

I\'m using Gradle\'s Application plugin to generate the install for a standalone java application. I have a configuration file I need to put on the classpath but I can\'t seem t

相关标签:
5条回答
  • 2021-02-01 19:35

    This isn't a particularly good answer to your question, but I've found a few instances where the startScripts task doesn't quite yet have the flexibility we need.

    I've worked around a couple of these instances by editing the file contents directly... not exactly taking advantage of Gradle's great model and not particularly recommended, but at least shows off how flexible Gradle is!

    So you could hack something into the classpath like this:

    tasks.startScripts {
      doLast {
        def scriptFile = file "${outputDir}/${applicationName}"
        scriptFile.text = scriptFile.text.replace('CLASSPATH=$APP_HOME/lib', 'CLASSPATH=$APP_HOME/conf/:$APP_HOME/lib')
      }
    }
    

    Be aware of breaking the start script's platform independence when doing this.

    0 讨论(0)
  • 2021-02-01 19:38

    Another workaround for this issue (GRADLE-2333) is proposed by Alexandr Fadeev here.

    Here is (a bit modified) Alexandr's solution that solved the problem for me on Gradle-1.6:

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

    It's a bit uglier than Josh's solution, but it allows you to preserve the exact directory layout (/src/dist/conf and $APP_HOME/conf) mentioned in the original question.

    0 讨论(0)
  • 2021-02-01 19:45

    It seems to me that what I'm trying to do shouldn't be that far out of the ordinary, but as a workaround I can have the dist directory be src/dist/lib/conf which allows the conf directory to be placed in the lib directory and the classpath that gradle generates for the sh/bat files to be correct.

    I'll accept another answer if someone has a better one.

    0 讨论(0)
  • 2021-02-01 19:49

    The easiest way to get a file onto the class path is to put it into src/main/resources. src/dist is for adding files to the distribution (i.e. zip file), not to the Jar/class path.

    0 讨论(0)
  • 2021-02-01 19:49

    I did a variant of Martin Dow's recipe below:

    I replace 'APP_HOME=' with 'export APP_HOME=' in the start script.

    Then my code can do System.env.get("APP_HOME") and then navigate to e.g. the conf/ folder.

    This is my Gradle hack:

    tasks.startScripts {
      doLast {
        def scriptFile = file "${outputDir}/${applicationName}"
        scriptFile.text = scriptFile.text.replaceAll('APP_HOME=',  'export APP_HOME=')
      }
    }
    

    Example of Java code in the app:

    String APP_HOME = System.env().get("APP_HOME");
    Properties p = new Properties();
    p.load(new FileInputStream(APP_HOME + "/conf/myapp.properties"))
    

    Hope this helps.

    NOTE: "export" does not work in Windows!

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