Adding project resources to Gradle Custom plugin

1.4k Views Asked by At

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?

1

There are 1 best solutions below

1
Marcell Lipusz On

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")