Basic grammar for defining tasks
There are two ways of definition, which we have used in the previous section:
task tName1 {
println '直接带闭包的定义方式'
}
task tName2() {
println '带括号的定义方式'
}
Review the conclusions from the experiment in the next section again, the above code will only be executed when building the project (gradle build), other methods are not executed.
If you need to execute the code when the task is called, you need to define the code in doFirst or doLast
Task dependent configuration
The three configuration methods are as follows:
task t1 {
doFirst {
println 't1'
}
}
//定义任务时参数依赖
task t2(dependsOn: 't1') {
doLast {
println 't2'
}
}
//任务内部依赖
task t3 {
dependsOn t1
doLast {
println 't3'
}
}
//任务外部依赖
task t4 {
doLast {
println 't4'
}
}
t4.dependsOn t1
Dynamic tasks (understand)
4.times { val ->
task "tk${val}" {
doLast {
println "The task is task${val}"
}
}
}
Refresh to view will generate 4 tasks
Customize properties for tasks
task t1 {
ext.myProperty = "Test property value"
doLast {
println "t1 ${myProperty}"
}
}
Perform the task to view the output:
task t1 {
ext.myProperty = "Test property value"
doLast {
println "t1 ${myProperty}"
}
}