了解 Gradle 构建生命周期以及每个阶段代表什么。
步骤 0. 开始之前
-
您已在 第 1 部分 中初始化了 Java 应用。
步骤 1. 了解构建生命周期
Gradle 构建有三个不同的阶段
- 阶段 1 - 初始化
-
在初始化阶段,Gradle 确定哪些项目将参与构建,并为每个项目创建一个
Project
实例。 - 阶段 2 - 配置
-
在配置阶段,使用构建中所有项目的构建脚本配置
Project
对象。Gradle 确定要执行的任务集。 - 阶段 3 - 执行
-
在执行阶段,Gradle 执行每个选定的任务。
当调用 Gradle 来执行任务时,生命周期就开始了。让我们看看实际情况。
步骤 2. 更新设置文件
将以下行添加到设置文件顶部
settings.gradle.kts
println("SETTINGS FILE: This is executed during the initialization phase")
settings.gradle
println('SETTINGS FILE: This is executed during the initialization phase')
步骤 3. 更新构建脚本
将以下行添加到构建脚本底部
app/build.gradle.kts
println("BUILD SCRIPT: This is executed during the configuration phase")
tasks.register("task1"){
println("REGISTER TASK1: This is executed during the configuration phase")
}
tasks.register("task2"){
println("REGISTER TASK2: This is executed during the configuration phase")
}
tasks.named("task1"){
println("NAMED TASK1: This is executed during the configuration phase")
doFirst {
println("NAMED TASK1 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK1 - doLast: This is executed during the execution phase")
}
}
tasks.named("task2"){
println("NAMED TASK2: This is executed during the configuration phase")
doFirst {
println("NAMED TASK2 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK2 - doLast: This is executed during the execution phase")
}
}
println("BUILD SCRIPT: This is executed during the configuration phase")
tasks.register("task1") {
println("REGISTER TASK1: This is executed during the configuration phase")
}
tasks.register("task2") {
println("REGISTER TASK2: This is executed during the configuration phase")
}
tasks.named("task1") {
println("NAMED TASK1: This is executed during the configuration phase")
doFirst {
println("NAMED TASK1 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK1 - doLast: This is executed during the execution phase")
}
}
tasks.named("task2") {
println("NAMED TASK2: This is executed during the configuration phase")
doFirst {
println("NAMED TASK2 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK2 - doLast: This is executed during the execution phase")
}
}
步骤 4. 运行 Gradle 任务
运行在步骤 3 中注册和配置的 task1
任务
$ ./gradlew task1
SETTINGS FILE: This is executed during the initialization phase (1)
> Configure project :app
BUILD SCRIPT: This is executed during the configuration phase (2)
REGISTER TASK1: This is executed during the configuration phase (2)
NAMED TASK1: This is executed during the configuration phase (2)
> Task :app:task1
NAMED TASK1 - doFirst: This is executed during the execution phase (3)
NAMED TASK1 - doLast: This is executed during the execution phase (3)
BUILD SUCCESSFUL in 25s
5 actionable tasks: 3 executed, 2 up-to-date
1 | 初始化:Gradle 执行 settings.gradle(.kts) 以确定要构建的项目,并为每个项目创建一个 Project 对象。 |
2 | 配置:Gradle 通过执行 build.gradle(.kts) 文件来配置每个项目。它解析依赖项并创建所有可用任务的依赖项图。 |
3 | 执行:Gradle 执行命令行中传递的任务和任何先决任务。 |
需要注意的是,虽然 task1
已配置并执行,但 task2
并未执行。这称为任务配置回避,可防止不必要的工作。
当调用 task1
且 task1
不依赖于 task2
时,Gradle 会回避配置 task2
,即任务配置回避。
下一步: 多项目构建 >>