Essential Groovy DSLs
[ 38 ]
Creating task dependencies
Any build tool is incomplete without the ability to create dependencies between
build tasks. With Gradle, we create dependencies using the dependsOn attribute.
We can create a dependency between two tasks as follows:
task helloSimple << {
println "Hello, Simple World!"
}
task helloWithDepends (dependsOn: 'helloSimple') << {
println "Hello, Dependent World!"
}
Invoking the helloWithDepends task will first cause the helloSimple task to
be executed.
$gradle -q -b scripts/ChapterThree/hello.gradle helloWithDepends
Hello, Simple World!
Hello, Dependent World!
Dependent tasks can be chained together by declaring one task to depend on another
task. This task in turn can already depend on a third task. We can also make a single
task dependent on multiple other tasks:
task clean << {
println 'Cleaning'
}
task runTests << {
println 'Running Tests'
}
task buildApplication (dependsOn: 'clean') << {
println 'Building Application'
}
task deploy (dependsOn: ['runTests','buildApplication']) << {
println 'Deploying'
}
When we run the preceding script, it produces the following output:
$gradle -q -b scripts/ChapterThree/default.gradle deploy
Cleaning
Building Application
Running Tests
Deploying
http://www.ebook3000.com