In a gradle build file, there are multiple ways to specify the items executed for a particular task. the doFirst method puts a task item on the top of the task stack, so that the item is executed before the rest of the task stack. This is very handy if you need to do some preparatory items before the main task. In fact, if you call doFirst several times, the item added in the last call is the first thing executed.
In theory, doLast should be doing something similar, but doLast should get called AFTER the main execution of the task is complete. But, in gradle 1.2, if you call doLast and then add something to the main task after doLast in the gradle.build file, the main task item is the last item called. For example, the following gradle build file:
task myTask myTask << { println "myTask main execution block" } myTask.doFirst { println "myTask doFirst call one" } myTask.doFirst { println "myTask doFirst call two" } myTask.doLast { println "myTask doLast" } myTask << { println "myTask more main execution block" }
Produces the following output:
:myTask myTask doFirst call two myTask doFirst call one myTask main execution block myTask doLast myTask more main execution block BUILD SUCCESSFUL Total time: 1.585 secs
My question is this: Is it the intent of doLast to simply append steps onto the end of the task (like doFirst tacks on to the beginning)? If so, doLast seems pointless with the exception of providing symmetry with doFirst. A user can simply do myTask << {...} to append something on to the end. I would have thought that doLast would make sure that any "doLast" items would be done after the main execution block.
Is this simply the way gradle's doLast is supposed to work, or is this a bug?? (or am I just stupid for appending something onto the main execution block after calling doLast -- which was a simple mistake after tacking in another execution block).
doFirst(action) Adds the given Action to the beginning of this task's action list. doLast(action) Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.
Gradle has different phases, when it comes to working with the tasks. First of all, there is a configuration phase, where the code, which is specified directly in a task's closure, is executed. The configuration block is executed for every available task and not only, for those tasks, which are later actually executed.
<<
is an alias for doLast
, so everything works as expected here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With