Task that dynamically depends on subproject's task

Hey,

I want to create a task let’s say “rootTask”, that dynamically according to some computation, depends on all tasks of type “Test” from some of the subprojects.

The problem is, that if this computation runs in the “doFirst” of the rootTask, then the taskGraph is already finalized. If i’m doing it in the configuration phase, then the subprojects’ tasks are not created yet.

Also, i’d prefer (though not a must) that the computation will only run if “rootTask” is being called.

What’s the best approach of doing this?

Hey,
I have a similiar problem.
How can you define task dependencies or input/output dependencies to a dynamically created task. As matans already says, either the task is not created yet, or it is too late for dependencies to be added.

I have tried adding dependencies in afterEvaluate and tasks.whenTaskAdded but both is too late. Adding a direct dependencies in the build script task.getByName(“dynamicTask”) will not work, as the task does not exist in that phase.

Just depend on the String “dynamicTask”, that will be resolved lazily and thus work for your case.

Thanks for the suggestion, however, the problem is not that I want a task to depend on the dynamically created task:
mytask.dependsOn(“dynamicTask”)

But I want the dynamic task to depend on my task
“dynamicTask”.dependsOn(mytask)

But I cannot get a reference to that dynamic task before it is configured and thus immutable.

But I want the dynamic task to depend on my task

Well, then you should no have said the exact opposite beofre. :wink:

tasks.withType<Foo>().matching { it.name == "dynamicTask" }.configureEach { dependsOn(mytask) }

If you don’t know the type, you can leave out the withType.