Get ClassLoader from gradle org.gradle.api.Project?

660 Views Asked by At

I'm writing a gradle plugin that wishes to read specific properties file from resources of a project of which this plugin is applied to. To read those resources, I need a classpath of the project. Currently I'm going with:

org.gradle.api.Project.getBuildscript().getClassLoader().getResourcesAsStream(...)

But it always returns null even though such resource exists in that project.

1

There are 1 best solutions below

0
On

Gradle buildscript is comilped and put into a local cache, so the project is not on the classpath of buildscript classloader. What you need is probably org.gradle.api.Project. absoluteProjectPath().

e.g. To read "src/main/resources/META-INF/MANIFEST.MF":

try(Reader in = new FileReader(project.absoluteProjectPath( "src/main/resources/META-INF/MANIFEST.MF" ))) {
    //...
}