Gradle构建具有conf文件夹的文件,其属性不是在jar中,而是在classpath中 [英] Gradle build file with conf folder with properties not in jar but on classpath
问题描述
您可以与配置文件夹conf共享build.gradle脚本示例:
- 包含所有包含文件的此文件夹已打包在gradle distZip中加入zip文件;
- 这个文件夹中的文件在类路径中(例如ClassLoader可访问)在startscripts文件中(在bin文件夹中);
- 这些文件在运行gradle run,gradle build等时也在classpath中。
加法#1: strong>
我已经完成了@wakjah指出的答案中提到的操作,文件被复制到zip文件中,并且在期间没有放入jar文件中gradle distZip
,这个涵盖了我的需求#1 。但是,该文件夹不在 bin
文件夹的启动脚本的classpath中(要求#2未覆盖 )。运行 gradle clean run
时(不包括 的要求3),文件不在类路径中。
加法#2:
我从零开始并借助 @wakjah 现在我有:
- 请求1:ok(目录
config
包含资源文件),但是这个资源也位于lib
文件夹中。 - 需求#2:好(目录
config
位于启动脚本的classpath中),但classpath还包含无效文件夹%APP_HOME%\lib\config
- 需求#3:好的
另外,我测试了3种加载文件内容的方法(如 Thread.currentThread()。getContextClassLoader()。getResource(name)成功。 在这里添加了我的示例项目zip文件: http://rgho.st/8r2dJjSz7 加法#3 : 当我在gradle脚本中注释/删除时: 在将 到 正如这里提到 Gradle distZip配置文件,您可以将文件夹 至于将它添加到运行时类路径中,添加文件夹作为依赖项(见下文)。 由于 以下代码收集了以上所有内容: 我已经测试过了,它似乎可以正常工作。在 编辑:在你的评论你暗示你也希望这可以在你的构建脚本的类路径中可用。如果是这样的话,你需要像这样添加一个构建脚本依赖: Could you please share a build.gradle script example with configured folder "conf": Addition #1: I've done actions mentioned in answer pointed by @wakjah, files was copied to a zip file and was not put into jar during Addition #2: I started sample project from scratch and with the help of @wakjah now I have: Additionally, I tested 3 approaches of loading file contents (as mentioned here), method Added my sample project zip file here: http://rgho.st/8r2dJjSz7 Addition #3: When I comment / remove in gradle script: Addition #4: After replacing to
As mentioned here Gradle distZip config files you can just add the folder As for adding it to the runtime classpath, just add the folder as a dependency (see below). Requirement #2 is a little harder, due to limitations with The following code collects all of the above: I have tested this and it appears to work. The correct classpath entries appear in both the run scripts and the command line used by gradle during the EDIT: In your comment you implied that you also want this to be available in the classpath of your build script. If that is the case, you need to add a buildscript dependency like this: EDIT #2: In another comment you mention that adding the runtime dependency has the unintended consequence of producing an extra, invalid, entry in the start script classpath. You could fix this by just adding to the classpath of any
这篇关于Gradle构建具有conf文件夹的文件,其属性不是在jar中,而是在classpath中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
依赖关系{
运行时文件('src / dist / config')
}
lib / config
文件夹不在CLASSPATH上(没关系),并且资源文件不会复制到 lib
文件夹中(没关系)。但是,资源文件无法加载Idea的启动类main()方法,并且在命令提示符下运行 gradle clean run
。在 gradle clean distZip
并部署(解包)应用程序后,资源会被加载。 / strong>:
依赖关系{
运行时文件('src / dist / config')
}
tasks.withType (JavaExec){
classpath + = files('src / dist / config')
}
一切都很顺利,谢谢, @wakjah !
还有一个问题,在最初的需求中没有提到:当我在Idea中启动这个项目时(不是执行gradle run,而是直接从Idea开始在类中启动main()),在 config无法使用前面提到的任何方法加载
目录。
dist / config
添加到您的 src
文件夹,它会自动包含在 distZip
任务中。
startScripts $ c $的限制,需求#2有点难度C>。但是,此问题有一个解决方法,提出了一种解决方法:使用Gradle的应用程序添加类路径条目插件
将配置文件放在src / dist / config中
依赖项{
运行时文件('src / dist / config')
}
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')
}
}
运行
任务期间,正确的类路径条目出现在运行脚本和gradle使用的命令行中。
buildscript {
dependencies {
classpath files('src / dist / config')
}
}
<编辑#2:在另一条评论中,您提到添加运行时依赖关系会在启动脚本类路径中产生额外的无效条目,这是意想不到的结果。您可以通过添加任何 JavaExec
任务的类路径来解决此问题,而不是使用依赖关系。所以用
依赖关系{runtime {...}}
> tasks.withType(JavaExec){
classpath + = files('src / dist / config')
}
gradle distZip
, this covers my requirement #1. But this folder is not on classpath in start scripts from bin
folder (requirement #2 is not covered). Files are not on classpath when running gradle clean run
(requirement #3 is not covered).
config
contains resource file), but this resource is also in lib
folder.config
is on classpath in start script), but classpath additionally contains invalid folder %APP_HOME%\lib\config
Thread.currentThread().getContextClassLoader().getResource(name)
succeeded.dependencies {
runtime files('src/dist/config')
}
lib/config
folder is not on CLASSPATH (it's ok), and resource file gets not copied to lib
folder (it's ok). But resource file is failed to load both starting class main() method from Idea and running gradle clean run
from command prompt. After gradle clean distZip
and deploying (unpacking) application, resource gets loaded.dependencies {
runtime files('src/dist/config')
}
tasks.withType(JavaExec) {
classpath += files('src/dist/config')
}
everything went fine, thanks, @wakjah!
There is one more issue, not mentioned in initial requirements: when I start this project inside Idea (not executing gradle run, but starting main() in class directly from Idea), file in config
directory could not be loaded using any of approaches mentioned earlier. dist/config
to your src
folder and it will be included by the distZip
task automatically.startScripts
. This question has an answer that proposes a workaround, however: Adding classpath entries using Gradle's Application plugin // put config files in src/dist/config
dependencies {
runtime files('src/dist/config')
}
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')
}
}
run
task.buildscript {
dependencies {
classpath files('src/dist/config')
}
}
JavaExec
tasks, rather than using a dependency. So replace the dependencies { runtime { ... } }
block withtasks.withType(JavaExec) {
classpath += files('src/dist/config')
}