内容来自:gradle in action
// gradle hello world demo
// create new file build.gradle
task helloworld {
doLast {
println 'hello world!'
}
}
# 命令行执行
$ gradle -q helloworld
hello world!
# 参数-q, --quiet Log errors only.
// demo2
task startSession {
doLast {
chant()
}
}
def chant() {
ant.echo(message: 'Repeat after me...') //这里隐含对Ant任务的使用
}
// 在循环里创建task,这里循环三次
// times 方法是 从 java.lang.Number 扩展的方法
// groovy 自动暴露了一个隐式变量 it 来指定循环迭代的次数
3.times {
task "yayGradle$it" {
doLast {
println 'Gradle rocks' // 动态任务的定义
}
}
}
//任务依赖的定义
//dependsOn 用来说明任务的依赖
//gradle 确保被依赖的task一定在定义该依赖的task之前执行
//实际上 dependsOn 是 task 的一个方法
yayGradle0.dependsOn startSession
yayGradle2.dependsOn yayGradle1, yayGradle0
task groupTherapy(dependsOn: yayGradle2)
#命令行执行
$ gradle groupTherapy
> Task :startSession
[ant:echo] Repeat after me...
> Task :yayGradle0
Gradle rocks
> Task :yayGradle1
Gradle rocks
> Task :yayGradle2
Gradle rocks
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
# 列出项目中所有可用的task
$ gradle -q tasks
------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------
Build Setup tasks #构建的setup任务帮助你初始化Grale的构建(比如:生成build.gradle文件)
-----------------
init - Initializes a new Gradle build. # 初始化任务
wrapper - Generates Gradle wrapper files.
Help tasks #帮助任务组 列出了任务的名字和他们的描述信息
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle'.
components - Displays the components produced by root project 'gradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle'.
dependentComponents - Displays the dependent components of components in root project 'gradle'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle'. [incubating]
projects - Displays the sub-projects of root project 'gradle'.
properties - Displays the properties of root project 'gradle'.
tasks - Displays the tasks runnable from root project 'gradle'.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
$ gradle -q tasks --all
------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle'.
components - Displays the components produced by root project 'gradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle'.
dependentComponents - Displays the dependent components of components in root project 'gradle'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle'. [incubating]
projects - Displays the sub-projects of root project 'gradle'.
properties - Displays the properties of root project 'gradle'.
tasks - Displays the tasks runnable from root project 'gradle'.
Other tasks # 没有分类的任务 没有被分配到一个任务组里面
# gradle提供了任务组的概念,可以看作是多个task的集合
# 如果某个task不属于某个任务组,那么就被分类到Other tasks中
# 后面学习如何将一个任务添加到一个任务组里面
-----------
groupTherapy #没有描述信息的任务没有自表达性,后面学习如何添加描述信息
prepareKotlinBuildScriptModel
startSession
yayGradle0
yayGradle1
yayGradle2
# gradle 可以一次执行多个task
$ gradle yayGradle0 groupTherapy
> Task :startSession
[ant:echo] Repeat after me...
> Task :yayGradle0
Gradle rocks
> Task :yayGradle1
Gradle rocks
> Task :yayGradle2
Gradle rocks
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
# 正确的任务被保存,而且每个任务只执行一次
# 通过task的驼峰缩写执行task
# 需要注意:保存缩写唯一
$ gradle yG0 gT
> Task :startSession
[ant:echo] Repeat after me...
> Task :yayGradle0
Gradle rocks
> Task :yayGradle1
Gradle rocks
> Task :yayGradle2
Gradle rocks
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
#排除一个任务执行
# 通过 -x 参数指定
zishi.ghq@DESKTOP-5208ROJ MINGW64 /d/study/gradle
$ gradle groupTherapy -x yayGradle0
> Task :yayGradle1
Gradle rocks
> Task :yayGradle2
Gradle rocks
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
# 结果发现,排除了task:yayGradle0和startSession, 这就是只能排除
网友评论