Controlling task configuration order

Hello.

I have a gradle build script where:

  • Task B dependsOn Task A
  • IThe configuration of Task B depends on extensions made in the configuration of Task A

tasks.register(“TaskA”) {
ext[“TaskAExt”] = SomeImportantObject
}

tasks.register(“TaskB”) {
dependsOn(“TaskA”)
val someImportantObject = ext[“TaskAExt”]
}

Unfortunately, it appears that, even though TaskB is executed after TaskA, TaskB is configured before TaskA. This causes the configuration of TaskB to error out with the message:

Cannot get property ‘TaskAExt’ on extra properties extension as it does not exist.

Is there any way to control the order of the configuration of the tasks, in particular so that TaskA is configured before TaskB?

try to register task B on the last steps of task A and like you did make B depend on A

tasks.register(“TaskA”) {
    ext[“TaskAExt”] = SomeImportantObject
    doLast {
        tasks.register(“TaskB”) {
        dependsOn(“TaskA”)
        val someImportantObject = ext[“TaskAExt”]
        }
    }
}

Don’t do this. Gradle has a “configuration” phase and an “execution” phase. A doLast {…} closure is running in the “execution” phase and by this time gradle has calculated the TaskGraph so it’s too late to be adding tasks to the model

See build phases