Add Output Directory To Source Sets from Custom Plugin

I’m working on the plugin that generate Java source code by thrift.
At Plugin’s extension, I wanna use

public abstract class ExampleExtension {
    public abstract DirectoryProperty getOutputDir();
    //.. and other properties

And at Plugin side, I wanna add it to source set when there is a Java plugin already.

public class ExamplePlugin implements Plugin<Project> {
    public void apply(Project project) {
        final ExampleExtension extension = ....;
        project.getPlugins().withType(JavaPlugin.class).configureEach(javaPlugin -> {

But looks like this doesn’t work due to sourceSet.getJava().srcDir(extension.getOutputDir()); will use the value before what user configured at build.gradle. Like the extension.getOutputDir() is fetch before user sets.

Curious if it’s good idea to add output to sourceset at plugin? Or I’m doing wrong… Thanks

Assuming you are also wiring that property to some task, better use the task as srcDir so that you also get the implicit task dependency whereever necessary automatically.

Btw. you can also do

public interface ExampleExtension {
    DirectoryProperty getOutputDir();
    //.. and other properties


You mean I can set sourceSet.getJava().srcDir(task) ?
Thanks for suggestion.

But this still use the default value instead of what we set at build.gradle.

Yes, that’s what I meant.

That’s pretty strange in both variants actually.
Maybe someone is eagerly resolving those paths.

Can you knit an MCVE?

1 Like

Ah it’s my fault.
I tried to print the sourceset by another task and looks like it doesn’t reflect it correctly.

So looks like my origin code works and your suggestion works too. When I run compileJava & reload at IDE, both reflect the correct folder in sourceset.

To make it has correct dependency, I will use your suggestion. Thank you.

1 Like