java 使用 Gradle 的应用程序插件添加类路径条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10518603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:23:59  来源:igfitidea点击:

Adding classpath entries using Gradle's Application plugin

javabuildgradle

提问by Josh

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 to get it to generate the classpath correctly in the sh/bat files. This configuration file needs to be located outside of the jar.

我正在使用 Gradle 的应用程序插件为独立的 Java 应用程序生成安装。我有一个需要放在类路径上的配置文件,但我似乎无法让它在 sh/bat 文件中正确生成类路径。此配置文件需要位于 jar 之外。

The conf file is in the directory /src/dist/conf/so when I run installAppit installs it under a conf directory like this $APP_HOME/conf.

conf 文件位于该目录中,/src/dist/conf/因此当我运行installApp它时,会将其安装在这样的 conf 目录下$APP_HOME/conf

I tried adding this directory to the claspath like this:

我尝试将此目录添加到 claspath 中,如下所示:

startScripts.classpath.add(files('$APP_HOME/conf'))

but when I look at the classpath in the sh/bat files it adds an entry that looks like this:

但是当我查看 sh/bat 文件中的类路径时,它添加了一个如下所示的条目:

$APP_HOME/lib/conf

How do I tell gradle to drop the libsection for this entry?

我如何告诉 gradle 删除lib此条目的部分?

采纳答案by Josh

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 distdirectory be src/dist/lib/confwhich allows the confdirectory to be placed in the libdirectory and the classpath that gradle generates for the sh/bat files to be correct.

在我看来,我尝试做的事情不应该太不寻常,但作为一种解决方法,我可以将dist目录设置src/dist/lib/conf为允许将conf目录放置在lib目录中,以及 gradle 生成的类路径sh/bat 文件是正确的。

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

如果有人有更好的答案,我会接受另一个答案。

回答by nehaev

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

Alexandr Fadeev在此处提出了针对此问题的另一种解决方法 ( GRADLE-2333) 。

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

这是(稍作修改)亚历山大的解决方案,它为我解决了 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.

它比 Josh 的解决方案更难看,但它允许您保留原始问题中提到的确切目录布局(/src/dist/conf 和 $APP_HOME/conf)。

回答by Peter Niederwieser

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

将文件放入类路径的最简单方法是将其放入src/main/resources. src/dist用于将文件添加到发行版(即 zip 文件),而不是添加到 Jar/class 路径。

回答by Martin Dow

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.

对于您的问题,这不是一个特别好的答案,但我发现了一些实例,其中 startScripts 任务还没有完全具备我们需要的灵活性。

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!

我已经通过直接编辑文件内容解决了几个这样的实例......没有完全利用 Gradle 的伟大模型,也不是特别推荐,但至少展示了 Gradle 是多么灵活!

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.

执行此操作时请注意破坏启动脚本的平台独立性。

回答by Olle Hallin

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

我做了以下 Martin Dow 食谱的变体:

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

我在启动脚本中将 'APP_HOME=' 替换为 'export APP_HOME='。

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

然后我的代码可以执行 System.env.get("APP_HOME") 然后导航到例如 conf/ 文件夹。

This is my Gradle hack:

这是我的 Gradle 黑客:

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:

应用程序中的 Java 代码示例:

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!

注意:“导出”在 Windows 中不起作用!