来源: http://www.yiibai.com/gradle/gradle_tasks.html
任务用于编译一些类,将类文件存储到单独的目标文件夹中,创建JAR,生成Javadoc或将一些归档发布到存储库。
定 义任务
task 是用于将任务定义到构建脚本( build.gradle )中的关键字。
//build.gradle
task hello {
doLast{
println 'hello world'
}
}
可以通过为 doLast 语句指定快捷方式(表示符号 << )来简化此 hello 任务。简化后脚本为:
//build.gradle
task hello << {
println 'hello world'
}
任务依赖关系
执行结果:task hello << { println "hello world " } task intro(dependsOn: hello) << { println "I'm Gradle" }
2) 也可以在task的配置区来声明它的依赖:
eg:
执行结果:task hello << { println "hello world" } task intro { dependsOn hello // 定义依赖,注意中间没有 ':' doLast { // 这种方式 不能使用 doLast的快捷方式 // 实际上 '<<' 快捷方式已经在Gradle 5.0+ 版本上被舍弃 println 'I'm Gradle' } }
3) 使用 dependsOn 函数调用
eg:
执行结果:task hello << { println "hello world" } task intro << { println "I'm Gradle" } intro.dependsOn hello // 声明依赖
添加任务描述
task copy(type:Copy){ description 'Copies the resource directory to the target directory.' from 'resources' into 'target' include('**/*.txt','**/*.xml','**/*.properties') println("description applied") }
![]()
task compile << { println("I'm compiling..."); } compile.doFirst { // here you would put arbitrary conditions in real life if(true) { throw new StopExecutionException() } } task myTask(dependsOn: 'compile'){ println("I'm not affected"); }
![]()