Difference between doLast and leftShift on Gradle

2019-04-29 10:26发布

问题:

I am aware of the difference between passing in a configuration closure and defining an action for a task. I believe you can't use << in a configuration closure because it seems like a syntax error:

task wrong {
  << { println "From doLast" }
}

But. Why can't I use leftShift as an equivalent of << in the above configuration closure? In an even more clear test case, why doesn't the following buildfile output the line From leftShift - inside?

defaultTasks 'tryout'

task tryout {
  doLast { println "From doLast" }
  leftShift { println "From leftShift - inside" }
}

tryout.leftShift { println "From leftShift - outside" }

Of course, this is not a real problem, as I can simply use doLast. I'm just trying to broaden my understanding of Gradle. Thanks!

回答1:

<< when used with a task definition, is not really a leftshift in the bitwise sense. It is shorthand for doLast. In gradle DSL:

task hello << {
    println 'Hello world!'
}

is exactly the same as:

task hello {
    doLast{
        println 'Hello world!'
    }
}


标签: gradle groovy