prashant
prashant

Reputation: 3608

What is the meaning of << in gradle task definition

What is the difference between these two tasks. Only task with << in its definition is shown in the output of ./gradlew tasks.

task greet(type: GreetingToFileTask) {
    destination = { project.greetingFile }
}

task sayGreeting(dependsOn: greet) << {
    println file(greetingFile).text
}

The lines above are from gradle documentation Here

Upvotes: 1

Views: 727

Answers (1)

Amnon Shochot
Amnon Shochot

Reputation: 9376

The << is a shortcut to the toLast configuration item of a task definition. I.e. the following two declarations are equivalent:

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

and:

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

(example taken from Gradle documentation here).

Now, in the first code snippet you just define a task and configuring its destination property. However, the task will only be executed if needed.

In the second code snippet, however, you are actually defining an action that will always be executed during the configuration phase, regardless of the tasks targeted for execution (cite from here):

A task has both configuration and actions. When using the <<, you are simply using a shortcut to define an action. Code defined in the configuration section of your task will get executed during the configuration phase of the build regardless of what task was targeted.

Upvotes: 2

Related Questions