了解 Gradle 构建生命周期及其每个阶段的含义。

在本节中,你将

  • 理解构建生命周期

  • 注册和配置两个任务

  • 运行任务查看各阶段的实际执行

步骤 0. 开始之前

  1. 你已经在第 1 部分中初始化了你的 Java 应用。

步骤 1. 理解构建生命周期

Gradle 构建有三个不同的阶段

阶段 1 - 初始化

在初始化阶段,Gradle 确定哪些项目将参与构建,并为每个项目创建一个 Project 实例。

阶段 2 - 配置

在配置阶段,Gradle 使用构建中所有项目的构建脚本来配置 Project 对象。Gradle 会确定要执行的任务集。

阶段 3 - 执行

在执行阶段,Gradle 执行每个选定的任务。

当调用 Gradle 执行任务时,生命周期开始。让我们实际操作一下。

gradle build lifecycle

步骤 2. 更新 Settings 文件

将以下行添加到 Settings 文件的顶部

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 没有。这被称为任务配置避免,它可以防止不必要的工作。

任务配置避免是指当调用 task1task1 不依赖于 task2 时,Gradle 会避免配置 task2

下一步: 多项目构建 >>