Java Gradle - 将目录添加到类路径

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

Gradle - add directory to classpath

javagradleclasspathbuild.gradle

提问by Omar Darwish

My application requires that a \configdirectory be available on the classpath when it looks for configurations files under the directory. I currently have dependencies configured like so, though this is probably not the correct way to make a directory available to my application:

当我的应用程序\config在目录下查找配置文件时,它要求在类路径上有一个目录可用。我目前有这样配置的依赖项,尽管这可能不是使目录可用于我的应用程序的正确方法:

dependencies {
    ... //runtime, compile dependencies pulled from repositories
    runtime files('config')
}

I am using the applicationplugin to create a standalone zip for my project. If my \configdirectory has \config\subdir, file1, file2, then the plugin produces a build\installdirectory with the following structure:

我正在使用该application插件为我的项目创建一个独立的 zip。如果我的\config目录有\config\subdir, file1, file2,那么插件会生成一个build\install具有以下结构的目录:

| build
| --|install
| ----|bin
| ------ projectName
| ------ projectName.bat
| ----|lib
| ------ dependency1.jar
| ------ dependency2.jar
| ------|subdir
| ------ file1
| ------ file2

This does not work for my application because it explicitly expects a \configdirectory

这对我的应用程序不起作用,因为它明确需要一个\config目录

However, this is the directory structure that I need:

但是,这是我需要的目录结构:

| build
| --|install
| ----|bin
| ------ projectName
| ------ projectName.bat
| ----|lib
| ------ dependency1.jar
| ------ dependency2.jar
| ----|config
| ------|subdir
| ------ file1
| ------ file2

How can I make gradle add another directory to the build and specify it as part of the classpath for the generated startup scripts?

如何让 gradle 将另一个目录添加到构建中并将其指定为生成的启动脚本的类路径的一部分?

采纳答案by RaGe

The application plugin documentationsays:

应用程序插件文档说:

Static files to be added to the distribution can be simply added to src/dist

要添加到分发中的静态文件可以简单地添加到 src/dist

I would try putting your config directory into src/dist/liband continue adding it to your classpath with runtime files('src/dist/lib/config')

我会尝试将您的配置目录放入src/dist/lib并继续将其添加到您的类路径中runtime files('src/dist/lib/config')

Note: working around this defectmeans that config has to go into /libunder src/dist

注:工作围绕这一缺陷意味着配置有进入/libsrc/dist

回答by Andy Malakov

You may try this:

你可以试试这个:

project('project-name') {
   apply plugin: 'application'
   mainClassName = "your.main.Class"

   startScripts {
       classpath += files('src/dist/lib/conf')
   }

More information can be found here.

可以在此处找到更多信息。