身為建置作者,您可以定義工作並指定它們之間的相依性。Gradle 保證工作將按照這些相依性所指示的順序執行。

您的建置腳本和外掛程式會配置此工作相依性圖。

例如,如果您的專案包含諸如 buildassemblecreateDocs 之類的工作,您可以配置建置腳本,以便它們按以下順序執行:buildassemblecreateDocs

工作圖

Gradle 在執行任何工作之前建立工作圖。

在建置中的所有專案中,工作形成有向無環圖 (DAG)。

此圖表顯示了兩個範例工作圖,一個是抽象的,另一個是具體的,任務之間的相依性以箭頭表示

task dag examples

外掛程式和建置腳本都透過工作相依性機制已註解的輸入/輸出來貢獻工作圖。

建置階段

Gradle 建置有三個不同的階段。

author gradle 1

Gradle 依序執行這些階段

階段 1. 初始化
  • 偵測 settings.gradle(.kts) 檔案。

  • 建立 Settings 實例。

  • 評估設定檔以確定哪些專案(和包含的建置)構成建置。

  • 為每個專案建立 Project 實例。

階段 2. 配置
  • 評估參與建置的每個專案的建置腳本 build.gradle(.kts)

  • 為請求的工作建立工作圖。

階段 3. 執行
  • 排程和執行選定的工作。

  • 工作之間的相依性決定執行順序。

  • 工作執行可以平行發生。

build lifecycle example

範例

以下範例顯示設定和建置檔案的哪些部分對應於各種建置階段

settings.gradle.kts
rootProject.name = "basic"
println("This is executed during the initialization phase.")
build.gradle.kts
println("This is executed during the configuration phase.")

tasks.register("configured") {
    println("This is also executed during the configuration phase, because :configured is used in the build.")
}

tasks.register("test") {
    doLast {
        println("This is executed during the execution phase.")
    }
}

tasks.register("testBoth") {
    doFirst {
        println("This is executed first during the execution phase.")
    }
    doLast {
        println("This is executed last during the execution phase.")
    }
    println("This is executed during the configuration phase as well, because :testBoth is used in the build.")
}
settings.gradle
rootProject.name = 'basic'
println 'This is executed during the initialization phase.'
build.gradle
println 'This is executed during the configuration phase.'

tasks.register('configured') {
    println 'This is also executed during the configuration phase, because :configured is used in the build.'
}

tasks.register('test') {
    doLast {
        println 'This is executed during the execution phase.'
    }
}

tasks.register('testBoth') {
	doFirst {
	  println 'This is executed first during the execution phase.'
	}
	doLast {
	  println 'This is executed last during the execution phase.'
	}
	println 'This is executed during the configuration phase as well, because :testBoth is used in the build.'
}

以下命令執行上面指定的 testtestBoth 工作。由於 Gradle 僅配置請求的工作及其相依性,因此 configured 工作永遠不會配置

> gradle test testBoth
This is executed during the initialization phase.

> Configure project :
This is executed during the configuration phase.
This is executed during the configuration phase as well, because :testBoth is used in the build.

> Task :test
This is executed during the execution phase.

> Task :testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
> gradle test testBoth
This is executed during the initialization phase.

> Configure project :
This is executed during the configuration phase.
This is executed during the configuration phase as well, because :testBoth is used in the build.

> Task :test
This is executed during the execution phase.

> Task :testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

階段 1. 初始化

初始化階段中,Gradle 偵測參與建置的專案集(根專案和子專案)和包含的建置。

Gradle 首先評估設定檔 settings.gradle(.kts),並實例化一個 Settings 物件。然後,Gradle 為每個專案實例化 Project 實例。

階段 2. 配置

配置階段中,Gradle 將工作和其他屬性新增至初始化階段找到的專案。

階段 3. 執行

執行階段中,Gradle 執行工作。

Gradle 使用配置階段產生的工作執行圖來決定要執行哪些工作。

下一步: 了解如何撰寫設定檔 >>