任务
Gradle 中Task 详解

我们可以通过很多种方式定义
task helloWorld << {
println "Hello World!"
}
这里的“«”表示向
gradle helloWorld
命令行输出如下:
:helloWorld
Hello World!
BUILD SUCCESSFUL
Total time: 2.544 secs
在默认情况下,
内置任务
gradle tasks
输出如下:
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
setupBuild - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
dependencies - Displays all dependencies declared in root project 'gradle-blog'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-blog'.
help - Displays a help message
projects - Displays the sub-projects of root project 'gradle-blog'.
properties - Displays the properties of root project 'gradle-blog'.
tasks - Displays the tasks runnable from root project 'gradle-blog'.
Other tasks
-----------
copyFile
helloWorld
To see all tasks and more detail, run with --all.
BUILD SUCCESSFUL
Total time: 2.845 secs
上面的
任务定义
task(hello) << {
println "hello"
}
task(copy, type: Copy) {
from(file('srcDir'))
into(buildDir)
}
//也可以用字符串作为任务名
task('hello') <<
{
println "hello"
}
task('copy', type: Copy) {
from(file('srcDir'))
into(buildDir)
}
//Defining tasks with alternative syntax
tasks.create(name: 'hello') << {
println "hello"
}
tasks.create(name: 'copy', type: Copy) {
from(file('srcDir'))
into(buildDir)
}
上述代码中使用了 <<
符号用来表征 {}
声明的动作是追加在某个任务的末尾,如果使用全声明即是:
task printVersion {
// 任务的初始声明可以添加 first 和 last 动作
doFirst {
println "Before reading the project version"
}
doLast {
println "Version: $version"
}
}
Default tasks(默认任务)
gradle
命令:
defaultTasks 'clean', 'run'
task clean << {
println 'Default Cleaning!'
}
task run << {
println 'Default Running!'
}
task other << {
println "I'm not a default task!"
}
> gradle -q
Default Cleaning!
Default Running!