Gradle custom task action order

I am looking at a simple example of a custom task in a Gradle build file from Gradle mastering by Manaka Mitra (page 70). Script line:

println "Working on custom task in build script"

class SampleTask extends DefaultTask {
    String systemName = "DefaultMachineName"
    String systemGroup = "DefaultSystemGroup"
    @TaskAction
    def action1() {
      println "System Name is "+systemName+" and group is "+systemGroup 
    }
    @TaskAction
    def action2() {
      println 'Adding multiple actions for refactoring'
    }

}

task hello(type: SampleTask)
hello {
    systemName='MyDevelopmentMachine'
    systemGroup='Development'
}

hello.doFirst {println "Executing first statement "} 
hello.doLast {println "Executing last statement "}

      

If I run the construct script with Gradle -q: hello, the output is as follows:

Executing first statement 
System Name is MyDevelopmentMachine and group is Development
Adding multiple actions for refactoring
Executing last statement 

      

As expected the first time the doFirst method was called, the two custom actions were executed in the order in which they were defined, and then the doLast action was executed. If I comment out the lines adding the doFirst and doLast actions, the output is:

Adding multiple actions for refactoring
System Name is MyDevelopmentMachine and group is Development

      

Custom actions are performed in the reverse order in which they are defined. I'm not sure why.

+3


source to share


1 answer


I think this is just a case where the ordering is not deterministic and you get different results depending on how you customize the problem further.

Why do you need two separate @TaskAction methods and not one that calls methods in a deterministic sequence? I can't think of any particular advantage of doing it this way (I understand it from the pattern in the book). Most of the other samples I find only have one method



@TaskAction 
void execute() {...} 

      

which I think makes more sense and is more predictable.

+2


source







All Articles