常规写法
代码语言:javascript复制task hello {
doLast {
println 'Hello world!'
}
}
简易写法
代码语言:javascript复制task hello << {
println 'Hello world!'
}
加载其他task
代码语言:javascript复制task hello << {
println 'Hello world!'
}
task intro(dependsOn: hello) << {
println "I'm Gradle"
}
执行命令: gradle -q intro
gradle -q intro Hello world! I’m Gradle
懒加载机制(加载在前,声明在后)
代码语言:javascript复制task taskX(dependsOn: 'taskY') << {
println 'taskX'
}
task taskY << {
println 'taskY'
}
执行命令: gradle -q taskX
gradle -q taskX taskY taskX
创建动态生成task
代码语言:javascript复制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相关内容
给动态生成的task添加依赖task
代码语言:javascript复制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
代码语言:javascript复制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的别名(效果相同)*
使用操作符获取tack相关信息
代码语言:javascript复制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中添加属性
代码语言:javascript复制task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties << {
println myTask.myProperty
}
执行命令: gradle -q printTaskProperties
gradle -q printTaskProperties myValue
使用Ant的task
代码语言:javascript复制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种提取出method
代码语言:javascript复制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
默认task
defaultTasks ‘clean’, ‘run’
代码语言:javascript复制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使用同一个变量时有不同的表现
代码语言:javascript复制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