Create task after extension container property is read

I have a gradle plugin, which adds an extension container.

I need to add a task based on a boolean flag on the extension container.

The issue is that the flag is always false, even if I set it to true. I guess that’s because the plugin tries to create the task before the extension properties have been read.

How can I execute some code after the extension properties have been read. Is afterEvaluate the only way?

//the plugin class MyPlugin implements Plugin {


void apply(Project project) {

project.extensions.create(‘my’, MyPluginExtension, project)

if ({


//at this point,shouldCreateTask is always false


} }

//the extension container public static class MyPluginExtension {

final Project project

MyPluginExtension(final Project project) {

this.project = project


def boolean shouldCreateTask = false; }

//the project

apply plugin ‘java’ apply plugin ‘my’

my {

shouldCreateTask=true }

I used a doFirst action which copies the value from the extension at the correct time (just before the task is executed). See:

This is the sort of thing that afterEvaluate is for. This is also something that you could potentially use a task rule for, too, if that makes more sense for what you are trying to do.