Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 3

[ 37 ]

The << operator is synonymous with doLast, so we can use it over again in the build
script to add actions to the end of the task. In this example, we create a task called
actionsInOrder. Once this task is created, we can continue to use << to action
actions to the task.


task actionsInOrder << {
println "The first will be first"
}
actionsInOrder << {
println "The last will be last"
}

When we run this script, the actions are executed in the order they are encountered
in the script as follows:


$gradle -q -b scripts/ChapterThree/hello.gradle actionsInOrder


The first will be first


The last will be last


Default tasks

Sometimes, in a build system, we need to define a default task or tasks to execute
whenever the build is run. We define default tasks in a Gradle build script with
the defaultTasks method. For instance, here we have two tasks; clean and
runTests, which we want to run by default whenever the build is executed
without a task specified.


defaultTasks 'clean', 'runTests'

task clean << {
println 'Cleaning'
}
task runTests << {
println 'Running Tests'
}

The preceding Gradle script when run produces the following output:


$gradle -q -b scripts/ChapterThree/default.gradle


Cleaning


Running tests

Free download pdf