Is project afterEvaluate the proper way for gradle plugin to dynamically create default tasks?

I try to avoid project.afterEvaluate as it gets messy when multiple plugins use it. It’s also hard for one plugin to intercept/tweak afterEvaluate logic from another plugin.

The TaskContainer extends DomainObjectCollection which has “live” methods such as all() and matching() etc. You could use these methods to disable a default task as soon as an override is added to the model.

Eg:

task defaultMyTask(type: MyTask) {
  // default stuff
} 
assemble.dependsOn defaultMyTask

tasks.all { task ->
   if (task.name != 'defaultMyTask' && task instanceof MyTask) {
      defaultMyTask.enabled = false
   } 
} 
2 Likes