简体   繁体   English

Gradle - 将目录添加到类路径

[英]Gradle - add directory to classpath

My application requires that a \\config directory be available on the classpath when it looks for configurations files under the directory.当我的应用程序在目录下查找配置文件时,它要求在类路径上有一个\\config目录可用。 I currently have dependencies configured like so, though this is probably not the correct way to make a directory available to my application:我目前有这样配置的依赖项,尽管这可能不是使目录可用于我的应用程序的正确方法:

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

I am using the application plugin to create a standalone zip for my project.我正在使用application插件为我的项目创建一个独立的 zip。 If my \\config directory has \\config\\subdir , file1 , file2 , then the plugin produces a build\\install directory with the following structure:如果我的\\config目录有\\config\\subdirfile1file2 ,那么插件会生成一个具有以下结构的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 \\config directory这对我的应用程序不起作用,因为它明确需要一个\\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 将另一个目录添加到构建中并将其指定为生成的启动脚本的类路径的一部分?

The application plugin documentation says:应用程序插件文档说:

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/lib and 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 defect means that config has to go into /lib under src/dist注意:解决这个缺陷意味着配置必须进入src/dist下的/lib

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 .可以在此处找到更多信息。

jar {
    manifest {
        attributes('Class-Path' : '<directory>' )
    }
}

The above worked for me以上对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM