Session 6
Ingmar Steiner
2017-06-14
Java
project.task("foo");
Groovy
project.task(foo)
Gradle DSL
task foo
task foo {
// task configuration
println "configuring foo"
doLast {
// task execution
println "executing foo"
}
}
task foo {
println "configuring foo"
doLast {
println "executing foo"
}
}
task bar {
dependsOn foo
println "configuring bar"
doLast {
println "executing bar"
}
}
task foo {
// declare output file
outputs.file 'foo'
doLast {
// create the file
println "creating file 'foo'"
file('foo').createNewFile() // we can always mix in java!
}
}
DefaultTask
sOf course, there are many common task types provided by the Gradle DSL
task copyFoo(type: Copy) {
from 'foo'
into 'fooCopy'
include '**/*.txt' // just text files, at any depth
expand properties
}
task wordCountFoo(type: Exec) {
commandLine 'wc', 'foo.txt'
def outputFile = file('wc.txt')
outputs.file outputFile
workingDir 'fooCopy'
standardOutput = new ByteArrayOutputStream()
doLast {
outputFile.text = standardOutput.toString()
}
}
Mix Groovy classes into DSL build script
class CreateFooFile extends DefaultTask {
@Input
String foo
@OutputFile
File outputFile
@TaskAction
void doStuff() {
outputFile.text = foo
}
}
task foo(type: CreateFooFile) {
foo = 'bar'
outputFile = file('baz/foo.txt')
}
@Input
/@Output
properties and files are “autowired” as task inputs/outputs@OutputFile
additionally creates any missing parent directoriesbuildSrc
projectbuildSrc
subdirectory will automatically build a Groovy project and provide its output to the main build classpath!buildSrc/src/main/groovy/CreateFooFile.groovy
, content as before:class FooPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('foo', type: CreateFooFile) {
foo = 'bar'
outputFile = project.file('baz/foo.txt')
}
}
}
class CreateFooFile extends DefaultTask {
@Input
String foo
@OutputFile
File outputFile
@TaskAction
void doStuff() {
outputFile.text = foo
}
}
apply plugin: FooPlugin
foo {
// override default configuration
foo = 'baz'
}
buildSrc
pluginsbuildSrc
for preconfigured custom build logic:buildSrc
└── src
└── main
└── groovy
├── CreateFooFile.groovy
└── FooPlugin.groovy
build.gradle
apply plugin: FooPlugin
Conventional plugin application: build.gradle
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath group: 'my.group', name: 'my-plugin', version: '0.1-SNAPSHOT'
}
}
apply plugin: 'my-plugin-id'
New application: settings.gradle
pluginRepositories {
maven {
url "${System.properties['user.home']}/.m2/repository"
}
}
build.gradle
plugins {
id 'my-plugin-id' version '0.1'
}
build.gradle
task sayAll
['foo', 'bar', 'baz'].each { thingie ->
task("say_$thingie") {
sayAll.dependsOn it
doLast {
println thingie
}
}
}
settings.gradle
build.gradle
files in subdirectories…settings.gradle
include 'foo', 'bar', 'baz'
build.gradle
subprojects {
task printName {
doLast {
println project.name
}
}
}
projects.txt
foo
bar
baz
settings.gradle
new File('projects.txt').eachLine { projName ->
include projName
}
build.gradle
subprojects {
task foo {
def outputFile = file("${project.name}.txt")
outputs.file outputFile
doLast {
outputFile.text = project.name
}
}
}
task dist(type: Zip) {
from subprojects.collect { project ->
project.foo
}
baseName 'dist'
}
build.gradle
class Sleep extends DefaultTask {
@TaskAction
void sleep() {
project.exec {
commandLine 'sleep', 2
}
}
}
subprojects {
task sleep(type: Sleep)
}
build.gradle
@ParallelizableTask
class Sleep extends DefaultTask {
@TaskAction
void sleep() {
project.exec {
commandLine 'sleep', 2
}
}
}
task sleepAll
(1..3).each { i ->
task("sleep_$i", type: Sleep) {
sleepAll.dependsOn it
}
}