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 {

@Override

void apply(Project project) {

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

if (project.my.shouldCreateTask){

createMyTask();

//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 }

1 Like

I used a doFirst action which copies the value from the extension at the correct time (just before the task is executed). See: http://forums.gradle.org/gradle/topics/how_to_pass_data_from_one_task_to_another

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.