了解 Gradle 构建生命周期以及每个阶段代表什么。
步骤 0. 开始之前
-
您在第 1 部分中初始化了 Java 应用程序。
步骤 1.了解构建生命周期
Gradle 构建分为三个不同的阶段:
- 第 1 阶段 - 初始化
-
在初始化阶段,Gradle 确定哪些项目将参与构建,并
Project
为每个项目创建一个实例。 - 第 2 阶段 - 配置
-
在配置阶段,
Project
使用构建中所有项目的构建脚本来配置对象。 Gradle 确定要执行的任务集。 - 第三阶段 - 执行
-
在执行阶段,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 任务
运行task1
您在步骤 3 中注册和配置的任务:
$ ./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
并未配置和执行。这称为任务配置避免并防止不必要的工作。
任务配置避免是指 Gradle 避免配置task2
何时task1
被调用且不task1
依赖。在task2
。
下一步: 多项目构建>>