eclipse 如何在 gradle 的应用程序插件“运行”任务的类路径上指定一个额外的文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30491345/
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-09-19 22:40:09  来源:igfitidea点击:

How do I specify an extra folder to be on the classpath for gradle's application plugin 'run' task?

javaeclipseintellij-ideagradleclasspath

提问by Mirrana

I've successfully configured my gradle build script to create a zip distribution of my application with an extra 'config' folder at the root. This folder contains (at least right now) only one properties file in use by the application, and is on the classpath for the application.

我已经成功地配置了我的 gradle 构建脚本来创建我的应用程序的 zip 发行版,在根目录下有一个额外的“config”文件夹。该文件夹(至少现在)仅包含一个应用程序正在使用的属性文件,并且位于应用程序的类路径中。

What I'm looking for now, however, is a way to do the same with the 'run' task in the application plugin. When I try to run my application this way, (for testing), my program fails to run because of a class trying to access this properties file on the root of the classpath.

然而,我现在正在寻找的是一种对应用程序插件中的“运行”任务执行相同操作的方法。当我尝试以这种方式运行我的应用程序时(为了测试),我的程序无法运行,因为一个类试图访问类路径根目录上的这个属性文件。

A bonus would be if I could get IntelliJ or Eclipse to also add this folder to its classpath just like the other folders (src/main/java, src/main/resources, ...) so I can run and debug my code from within the IDE without invoking a gradle task. I want to try to avoid as much as possible tying this code to any one IDE, so that when anybody needs to work on the project, they just need to import the build.gradle file and have the IDE make the appropriate config files it needs.

如果我可以让 IntelliJ 或 Eclipse 也像其他文件夹(src/main/java、src/main/resources 等)一样将此文件夹添加到其类路径中,那么我就可以运行和调试我的代码在 IDE 中,无需调用 gradle 任务。我想尽量避免将此代码绑定到任何一个 IDE,这样当任何人需要处理该项目时,他们只需要导入 build.gradle 文件并让 IDE 制作它需要的适当配置文件.

Here is my build.gradle file:

这是我的 build.gradle 文件:

apply plugin: 'application'

mainClassName = "MainClass"

startScripts {
    // Add config folder to classpath. Using workaround at
    // https://discuss.gradle.org/t/classpath-in-application-plugin-is-building-always-relative-to-app-home-lib-directory/2012
    classpath += files('src/dist/config')
    doLast {
        def windowsScriptFile = file getWindowsScript()
        def unixScriptFile = file getUnixScript()
        windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\lib\config', '%APP_HOME%\config')
        unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/config', '$APP_HOME/config')
    }
}

repositories {
    ...
}

dependencies {
    ...
}

Likely what needs to happen is that I need to have the /src/dist/config folder to be copied into the build directory and added to the classpath, or have its contents be copied into a folder that is already on the classpath.

可能需要发生的是,我需要将 /src/dist/config 文件夹复制到构建目录并添加到类路径中,或者将其内容复制到类路径上已有的文件夹中。

回答by Mirrana

I ended up taking Opal's suggestion as a hint, and came up with the following solution. I added the following to my build.gradle file:

我最终将 Opal 的建议作为提示,并提出了以下解决方案。我在 build.gradle 文件中添加了以下内容:

task processConfig(type: Copy) {
    from('src/main/config') {
        include '**/*'
    }

    into 'build/config/main'
}

classes {
    classes.dependsOn processConfig
}

run {
    classpath += files('build/config/main')
}

Alternatively, a simpler approach would be to add a runtime dependency to my project as such:

或者,一种更简单的方法是将运行时依赖项添加到我的项目中:

dependencies {
    ...
    runtime files('src/main/config')
}

I didn't end up doing it this way, however, because my distribution package ended up having .properties files in the lib folder... and I'm just picky that way.

然而,我最终并没有这样做,因为我的分发包最终在 lib 文件夹中有 .properties 文件……而我就是那样挑剔。

回答by Opal

As you can see in the docsrunis a task of type JavaExec. So classpathfor it can be modified. Try to add configfolder to the classpath. See here.

正如您在文档中看到的,run是一个类型为的任务JavaExec。因此可以修改它的类路径。尝试将config文件夹添加到类路径。见这里