Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle doFirst() Execution Order

Tags:

gradle

groovy

How is the order of the doFirst method determined in a gradle build script? I have the following sample script that contains two doFirst methods. I understand that they are additive, as they both execute, but the order that this occurs looks backward:

task initialize
task depTask(dependsOn: initialize)

initialize {
    doFirst {
        println 'processing doFirst in initialization (configuration)'
    }

    println 'processing initialize (configuration)'
}

depTask {
    println 'processing depTask (configuration)'
}

depTask << {
    println 'executing depTask (execution)'
}

initialize << {
    println 'executing initialize (execution)'
}

initialize.doFirst {
    println 'executing doFirst on initialize (execution)'
}

The output from this script is:

processing initialize (configuration)
processing depTask (configuration)
executing doFirst on initialize (execution)
processing doFirst in initialization (configuration)
executing initialize (execution)
executing depTask (execution)

The first "doFirst" function is defined in the initialize task. The second is defined outside of the configuration block. Why doesn't the first instance execute before the second one? The order of execution looks backward. I would have expected the first one, inside the configuration definition, to execute first. Any help understanding this would be appreciated.

like image 316
jmq Avatar asked Feb 09 '12 00:02

jmq


1 Answers

initialize { doFirst { ... } } and initialize.doFirst { ... } are the exact same thing. Both statements are inserting an action at the front of the task's action list. Hence the action that gets inserted later (in this case further down in the script) will get executed first.

like image 84
Peter Niederwieser Avatar answered Oct 11 '22 05:10

Peter Niederwieser