1

I am building a Custom gradle plugin which when applied to the projects will use the configuration files in the project resources folder to fill some templates and generate some other configuration files.

But when I read the files in my plugin as classpath resources, it fails with cant find the File.

public class VideoBuildPlugin implements Plugin<Project> {

    @Override
    public void apply(Project target) {
        String file = "app/config/dev.yml"; // These files reside in the resources of the project folder
        VideoBuildPlugin.class.getClassLoader().getResourceAsStream(file); // This line fails
    }
}

Do I have to do, to add the project resources to the classpath of the build plugin to get this working?

Praneeth Ramesh
  • 3,434
  • 1
  • 28
  • 34

1 Answers1

0

You can add explicitly files to the buildscript classpath(in your app where you apply the custom plugin):

buildscript {
    dependencies {
        classpath files("src/main/resources")
    }
}

Although I'm not sure if this is the best approach.

Alternatively: You can access the "resources" folder in the main sourceSet

        def javaPlugin = project.convention.getPlugin(JavaPluginConvention.class)

        def mainSourceSet = javaPlugin.sourceSets.getByName("main")
        def resources = mainSourceSet.getResources()

        resources.srcDirs[0].resolve("app/config/dev.yml")
  • Is there any way that I could just add these main resources to the Build ClassLoader so that getResourceAsStream(file) directly works for me.. I am using some legacy code which I am calling from my build plugin. I am not allowed to modify this legacy code. – Praneeth Ramesh Jul 05 '19 at 23:29