Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle leftshift << operator with Task needed? (Is it superfluous)

Tags:

gradle

I am maintaining gradle code that sometimes uses the leftshift operator << in defining task. It works fine, but so does omitting the << operator from the task.

I understand the purpose of the left shift operator as explained What's the operator << (double less than) in gradle?

so... I get the purpose of leftshift operator <<. It is to add to a set of actions for a task and when the task is ran it will execute the actions in the same order in which the the task were entered. I Get it and I can see that it works properly. However, omitting << will result in the same behavior. I can see where using << in Gradle makes sense, but in the case of tasks it seems to be just superfluous and should be omitted. Is that correct or does the leftshift operator serve a purpose.

Example:

task Foo
task Bar

Foo << {
  println "foo action 1"
}

Foo << {
  println "foo action 2"
}

Bar  {
  println "bar action 1"
}

Bar  {
  println "bar action 2"
}

Foo and Bar behave exactly the same.

like image 423
Stephen Avatar asked Nov 16 '15 02:11

Stephen


People also ask

What is << In gradle?

In gradle << operator is used to add action to a particular task. A task consists of multiple actions that are run (in order they were added) during the execution of the task. << just adds an action to tasks collection of actions. More about tasks and actions can be found here.

How do I disable tasks in gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

What is gradle task type?

Gradle supports two types of task. One such type is the simple task, where you define the task with an action closure. We have seen these in Build Script Basics. For this type of task, the action closure determines the behaviour of the task. This type of task is good for implementing one-off tasks in your build script.

How do I enable tasks in gradle?

Configure running triggers for Gradle tasks IntelliJ IDEA lets you run Gradle tasks before your project's execution or set other conditions using the task activation configuration. In the Gradle tool window, right-click a Gradle project. From the context menu, select Tasks Activation.


2 Answers

The left shift (<<) operator is an alias to Task.doLast(), meaning that it adds an action to the task. A task action being some code that is evaluated when the task executes. Omitting the operator simply configures the task. The distinction being that one runs at configuration time (when Gradle runs your build script) and the other at execution time.

Essentially this example

task foo << { 
    println 'bar' 
}

is equivalent to

task foo {
    doLast {
        println 'bar'
    }
}
like image 170
Mark Vieira Avatar answered Nov 23 '22 17:11

Mark Vieira


Mark Vieira's answer is exactly right, but I would also add for clarification, that without the << operator, the code in your closure will be executed when the script is parsed, rather than when it is executed.

This is most obvious when you run the "clean" task. If you don't use the << for all your tasks, then gradle clean will actually execute the code in all your tasks.

Try this:

build.gradle:

apply plugin: 'groovy'

task foo { println 'foo'}

and now try running the clean task

$>gradle clean
14:23:57: Executing external task 'clean'...
foo
:clean UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.216 secs
14:23:57: External task execution finished 'clean'.`

Now, if you add the <<,

build.gradle:

apply plugin: 'groovy'

task foo << { println 'foo'}

it will do this:

$>gradle clean
14:27:28: Executing external task 'clean'...
:clean UP-TO-DATE

BUILD SUCCESSFUL

Total time: 3.547 secs
14:27:32: External task execution finished 'clean'.

Note that the leftshift << operator was deprecated and is no longer in gradle versions 5.0 and up. You can replace the code above with:

build.gradle:

task foo { doLast { println 'foo' } }
like image 45
Aaron Knauf Avatar answered Nov 23 '22 17:11

Aaron Knauf