Gradle: Add a resource in Plugin-Jar

2019-09-03 03:17发布

问题:

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?

回答1:

ClassLoader.getResourceAsStream() is an instance method, not a static method. (I'd expect Groovy to tell you that, in one way or another.) Try getClass().classloader.getResourceAsStream() or buildscript.classLoader.getResourceAsStream().



标签: gradle