了解 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
時,Gradle 會避免組態 task2
,而且 task1
並不依賴於 task2
。
下一步: 多專案建置 >>