Adding classpath entries using Gradle's Application plugin

后端 未结 5 513
攒了一身酷
攒了一身酷 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: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!

提交回复
热议问题