作为构建作者,您可以定义任务以及任务之间的依赖关系。 Gradle 保证这些任务将按照其依赖关系的顺序执行。
您的构建脚本和插件配置此依赖关系图。
例如,如果您的项目任务包括build
、assemble
、createDocs
,您的构建脚本可以确保它们按照build
→ assemble
→ 的顺序执行createDoc
。
任务图
Gradle 在执行任何任务之前构建任务图。
在构建中的所有项目中,任务形成有向无环图(DAG)。
该图显示了两个示例任务图,一个是抽象的,另一个是具体的,任务之间的依赖关系用箭头表示:
插件和构建脚本都通过任务依赖机制和带注释的输入/输出对任务图做出贡献。
构建阶段
Gradle 构建具有三个不同的阶段。
Gradle 按顺序运行这些阶段:
- 第 1 阶段. 初始化
- 第 2 阶段. 配置
-
-
build.gradle(.kts)
评估参与构建的每个项目的构建脚本。 -
为请求的任务创建任务图。
-
- 第三阶段:执行
-
-
安排并执行选定的任务。
-
任务之间的依赖关系决定了执行顺序。
-
任务的执行可以并行发生。
-
例子
以下示例显示了设置和构建文件的哪些部分对应于各个构建阶段:
settings.gradle.kts
rootProject.name = "basic"
println("This is executed during the initialization phase.")
build.gradle.kts
println("This is executed during the configuration phase.")
tasks.register("configured") {
println("This is also executed during the configuration phase, because :configured is used in the build.")
}
tasks.register("test") {
doLast {
println("This is executed during the execution phase.")
}
}
tasks.register("testBoth") {
doFirst {
println("This is executed first during the execution phase.")
}
doLast {
println("This is executed last during the execution phase.")
}
println("This is executed during the configuration phase as well, because :testBoth is used in the build.")
}
settings.gradle
rootProject.name = 'basic'
println 'This is executed during the initialization phase.'
build.gradle
println 'This is executed during the configuration phase.'
tasks.register('configured') {
println 'This is also executed during the configuration phase, because :configured is used in the build.'
}
tasks.register('test') {
doLast {
println 'This is executed during the execution phase.'
}
}
tasks.register('testBoth') {
doFirst {
println 'This is executed first during the execution phase.'
}
doLast {
println 'This is executed last during the execution phase.'
}
println 'This is executed during the configuration phase as well, because :testBoth is used in the build.'
}
以下命令执行上面指定的test
和testBoth
任务。由于 Gradle 仅配置请求的任务及其依赖项,因此configured
任务从不配置:
> gradle test testBoth
This is executed during the initialization phase.
> Configure project :
This is executed during the configuration phase.
This is executed during the configuration phase as well, because :testBoth is used in the build.
> Task :test
This is executed during the execution phase.
> Task :testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
> gradle test testBoth
This is executed during the initialization phase.
> Configure project :
This is executed during the configuration phase.
This is executed during the configuration phase as well, because :testBoth is used in the build.
> Task :test
This is executed during the execution phase.
> Task :testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
第 1 阶段. 初始化
在初始化阶段,Gradle 检测项目集(根项目和子项目)并包含参与构建的构建。
Gradle 首先评估设置文件,settings.gradle(.kts)
并实例化一个Settings
对象。然后,Gradle 实例化Project
每个项目的实例。
第 2 阶段. 配置
在配置阶段,Gradle 将任务和其他属性添加到初始化阶段找到的项目中。