Get all sourceSets from root project

plugins

(Paul Baker) #1

I’m writing a plugin to do some custom file indexing for an internal use-case we have where I currently work.

Doing some tinkering I’ve found that I can create my task/plugin in the rootProject’s buildSrc apply a task to each module via

subprojects {
    apply plugin: MyCustomIndexerPlugin
}

My plugin’s implementation looks like this, and works just fine in the context of that single module:

@Override
public void apply(Project project) {
    Convention convention = project.getConvention();
    System.out.println("Working: " + project.getName());
    JavaPluginConvention javaPluginConvention = convention.getPlugin(JavaPluginConvention.class);
    SourceSetContainer sourceSets = javaPluginConvention.getSourceSets();

    TaskContainer taskContainer = project.getTasks();
    MyCustomIndexerTask myCustomIndexerTask = taskContainer.create("myCustomIndexerTask", MyCustomIndexerTask.class, task -> task.setSourceSetContainer(sourceSets));

    Task build = taskContainer.getByName("build");
    build.dependsOn(myCustomIndexerTask);
}

And here is my task:

@TaskAction
public void taskAction() {
    SortedMap<String, SourceSet> asMap = getSourceSetContainer().getAsMap();
    for (String sourceSetName : asMap.keySet()) {
        SourceSet sourceSet = asMap.get(sourceSetName);
        Set<File> files = sourceSet.getAllJava().getFiles();
        for (File file : files) {
            System.out.println(sourceSetName + " -> " + file);
        }
    }
}

This is (sorta) okay as a proof of concept, but I’d like to have my custom task performed at the rootProject level. So after all modules build successfully I run my code against all sourceSets. Is this possible or do I need to somehow pass this data from module to module as my project builds?

I’m having a difficult time finding the right documentation to do the right meta-coding I need to perform.