Gradle:在Plugin-Jar中添加资源 [英] Gradle: Add a resource in Plugin-Jar
问题描述
我有一个Gradle插件,它被构建为Jar并上传到存储库。现在我想将配置文件移动到一个插件的src / main / resources文件夹中。但是我在项目评估过程中无法掌握资源,我怀疑这是因为我仅在buildscript块中引用了它。
项目的buildscript基本上看起来像像这样
buildscript {
pre>
repositories {mavenRepo url:http:// ...}
依赖关系{classpathgradle:loadtest-plugin:0.1.0}
}
apply plugin:loadtest-plugin
在loadtest-plugin.jar里面有一个src / main / resources / config.txt,我想从插件里面访问,像这样:
def config = ClassLoader.getResourceAsStream(config.txt)。text
但我只能得到NullPointerExceptions。任何想法?
解决方案ClassLoader.getResourceAsStream()是一个实例方法,而不是一个静态的方法。 (我希望Groovy以这种或那种方式告诉你。)尝试
getClass()。classloader.getResourceAsStream()
或buildscript。 classLoader.getResourceAsStream()
。I have a Gradle plugin, that gets built as Jar and uploaded to a repository. Now I would like to move a configuration file into the src/main/resources folder of one plugin. But I can't get hold of the resource during project evaluation, I suspect it's because I am referencing it only in the buildscript { } block.
The project's buildscript essentially looks like this
buildscript { repositories { mavenRepo url: "http://..." } dependencies { classpath "gradle:loadtest-plugin:0.1.0" } } apply plugin: "loadtest-plugin"
Inside the loadtest-plugin.jar there is a src/main/resources/config.txt which I would like to access from inside the plugin, like this:
def config = ClassLoader.getResourceAsStream("config.txt").text
But I only get NullPointerExceptions. Any Ideas?
解决方案
ClassLoader.getResourceAsStream()
is an instance method, not a static method. (I'd expect Groovy to tell you that, in one way or another.) TrygetClass().classloader.getResourceAsStream()
orbuildscript.classLoader.getResourceAsStream()
.这篇关于Gradle:在Plugin-Jar中添加资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文