You can’t attach anything to a task by name if it has not yet been declared (deferred configuration doesn’t change anything here, it still has to be declared first, even if not yet realized).
To get close to this, you will need to hook into the task container lifecycle so that your code executes after the task is actually added (code before the afterEvaluate
block executes after the task.register('foo')
executes). Without doing this, the order of code in the file still matters (both using afterEvaluate
, for example).
tasks.whenTaskAdded {
if (it.name == 'foo') {
// configure it here
}
}
tasks.withType(DefaultTask) { // Nearly identical to above, but might be preferred if you can be more specific about task type
if (it.name == 'foo') {
// configure it here
}
}
The only potential concern is that this code will run before the configuration added when the task is registered (i.e. println("configuring foo")
).