task hello {
doLast {
println 'Hello world!'
}
}
task hello << {
println 'Hello world!'
}
task hello << {
println 'Hello world!'
}
task intro(dependsOn: hello) << {
println "I'm Gradle"
}
执行命令: gradle -q intro
gradle -q intro Hello world! I’m Gradle
task taskX(dependsOn: 'taskY') << {
println 'taskX'
}
task taskY << {
println 'taskY'
}
执行命令: gradle -q taskX
gradle -q taskX taskY taskX
4.times { counter ->
task "task$counter" << {
println "I'm task number $counter"
}
}
执行命令: gradle -q task1
gradle -q task1 I’m task number 1 这种情况无法使用 &task名字. 这种模式访问task相关内容
4.times { counter ->
task "task$counter" << {
println "I'm task number $counter"
}
}
task0.dependsOn task2, task3 执行命令: gradle -q task0
gradle -q task0 I’m task number 2 I’m task number 3 I’m task number 0
task hello << {
println 'Hello Earth'
}
hello.doFirst {
println 'Hello Venus'
}
hello.doLast {
println 'Hello Mars'
}
hello << {
println 'Hello Jupiter'
}
执行命令: gradle -q hello
gradle -q hello Hello Venus Hello Earth Hello Mars Hello Jupiter * “<<”操作符实际上是doLast的别名(效果相同)*
task hello << {
println 'Hello world!'
}
hello.doLast {
println "Greetings from the $hello.name task."
}
执行命令: gradle -q hello
gradle -q hello Hello world! Greetings from the hello task.
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties << {
println myTask.myProperty
}
执行命令: gradle -q printTaskProperties
gradle -q printTaskProperties myValue
task loadfile << {
def files = file('../antLoadfileResources').listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
执行命令: gradle -q loadfile
gradle -q loadfile * agile.manifesto.txt * Individuals and interactions over processes and tools Working software over comprehensive documentation Customer collaboration over contract negotiation Responding to change over following a plan * gradle.manifesto.txt * Make the impossible possible, make the possible easy and make the easy elegant. (inspired by Moshe Feldenkrais)
task checksum << {
fileList('../antLoadfileResources').each {File file ->
ant.checksum(file: file, property: "cs_$file.name")
println "$file.name Checksum: ${ant.properties["cs_$file.name"]}"
}
}
task loadfile << {
fileList('../antLoadfileResources').each {File file ->
ant.loadfile(srcFile: file, property: file.name)
println "I'm fond of $file.name"
}
}
File[] fileList(String dir) {
file(dir).listFiles({file -> file.isFile() } as FileFilter).sort()
}
执行命令: gradle -q loadfile
gradle -q loadfile I’m fond of agile.manifesto.txt I’m fond of gradle.manifesto.txt
defaultTasks ‘clean’, ‘run’
task clean << {
println 'Default Cleaning!'
}
task run << {
println 'Default Running!'
}
task other << {
println "I'm not a default task!"
}
执行命令: gradle -q
gradle -q Default Cleaning! Default Running! 如果子task没有定义默认的task,而父task指定,则子task会继承父task的默认task
task distribution << {
println "We build the zip with version=$version"
}
task release(dependsOn: 'distribution') << {
println 'We release now'
}
gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(release)) {
version = '1.0'
} else {
version = '1.0-SNAPSHOT'
}
}
执行命令: gradle -q distribution
gradle -q distribution We build the zip with version=1.0-SNAPSHOT 执行命令: gradle -q release gradle -q release We build the zip with version=1.0 We release now