HelloWorld
HelloWorld

Reputation: 617

Understanding a subtlety of Gradle/Groovy tasks

I get an error when running the build script:

task pullInDeps(dependsOn: copyMod, description: 'Pull in all the module dependencies for the module into the nested mods directory') {
if (pullInDeps == 'true') {
    setSysProps()
    def args = ['pulldeps', moduleName]
    Starter.main(args as String[])
}
}

But, I do not get an error when running:

task pullInDeps(dependsOn: copyMod, description: 'Pull in all the module dependencies for the module into the nested mods directory') << {
    if (pullInDeps == 'true') {
        setSysProps()
        def args = ['pulldeps', moduleName]
        Starter.main(args as String[])
    }
}

Note: The difference is the << when defining the task. Also, note that if the former is done with doLast{} surrounding the if statement it works. AND it works when doFirst{} is used

--This is from the vert.x gradle-template-example (but adding it to my own project).

I'm really just trying to understand gradle/groovy better as I've already solved the problem.

EDIT:

ERROR:

* What went wrong:
A problem occurred evaluating script.
> Could not find property 'Starter' on task ':pullInDeps'.

I'm not sure why leftShift or doFrist/Last() makes a difference for Starter.

Upvotes: 1

Views: 2837

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

The concerned task has an action tied to it ( logic inside the closure ) which is represented with the use of leftShift operator. Here is the actual semantic:

task pullInDeps << { task action }

The task itself is passed into the closure as a parameter to be used in order to define an action.

This is synonymous to doFirst { } and doLast { } which takes the task itself as parameter.

In case when you define the task as:

task pullInDeps { }

the task itself will be configured instead of defining any action, hence the task itself as parameter is not available in the closure.

Refer the second paragraph in Task Actions.

Upvotes: 2

Related Questions