Adding dynamically a task to the execution plan

Hi,

I am writing a plugin which provides several custom tasks. I have one task B, which should be executed after another task A when some dynamic conditions (some configurations within an extendsion) are met. Task B depends on Task A but when Task A is called, task B will not be executed.

It’s a bit like finalizedBy (but not the same), because this task must not be ordered with other finalization tasks, but with the regular tasks to be executed.

Is there a possibility to add a task dynamically to the execution plan?

Regards,

Frito

Have you considered using ‘onlyIf’ ?

// Assuming these tasks
task A
task B
  B {
  dependsOn A
    onlyIf {
     //place some condition(s) in here i.e.
    project.configurations.getByName('myConfig').contains('foo')
  }
}

Thanks for your answer.

That’ s not exactly what I am looking for. The tasks are provided by a Gradle plugin. The caller does not know anything about task B and always calls Task A.

I have to evaluate some conditions within the configuration phase and have to tell gradle to add task B to the execution plan and add the dependency from Task B onto Task A.

You could use ‘project.afterEvaluate’.

task B
  project.afterEvaluate {
  if( your_conditions_are_met ) {
    A.dependsOn B
  }
}

Any closure added with ‘afterEvaluate’ will be called at the end of the configuration phase.

Sorry, I probably could not make clear that a dependency won’t work.

A depending on B would be the wrong execution order.

B depending on A would match the execution order, but as I tried to make clear: the user does not know anything about task B and only calls task A. Using a simple dependency would not cause task B to be executed when task A is called.

What I need is “adding a new task (task B) to the tasks to be executed and make B dependent on A”, when only Task A is called and some special conditions are met. This must be done during my plugin is being applied.

Best Regards,

Frito

My solution for now is an empty (noop) task A depending on the “real” task A’ .

If the conditions are met, I can create a new Task B depending on A’ and make task A dependent on task B.

But I am looking for something better…