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

在本节中,您将

  • 了解构建生命周期

  • 注册和配置两个任务

  • 运行任务以查看实际运行中的阶段

步骤0. 开始之前

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

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

Gradle构建有三个不同的阶段

阶段1 - 初始化

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

阶段2 - 配置

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

阶段3 - 执行

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

当调用Gradle执行任务时,生命周期就开始了。让我们看看它的实际运行。

gradle build lifecycle

步骤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

下一步: 多项目构建 >>