第 3 部分:相依性管理
了解專案相依性和 Gradle 相依性管理的基礎知識。
步驟 1. 瞭解版本目錄
版本目錄用於在集中位置宣告專案的所有直接相依性。
它是由 Gradle 初始化在 gradle/libs.versions.toml
中建立,並在子專案建置檔案中參照。
[versions]
guava = "32.1.2-jre"
junit-jupiter = "5.10.0"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
要參照版本目錄中定義的函式庫
dependencies {
implementation(libs.guava)
testImplementation(libs.junit.jupiter)
}
dependencies {
implementation libs.guava
testImplementation libs.junit.jupiter
}
在此範例中,libs.guava
和 libs.junit.jupiter
參照版本目錄中定義的對應函式庫。
使用 versions.junit.jupiter 語法,因為金鑰包含連字號,而連字號是特殊字元。
|
版本目錄提供許多優點,勝過直接在建置指令碼中宣告相依性
-
Gradle 從目錄產生類型安全的存取器,讓您可以在 IDE 中輕鬆新增相依性,並享有自動完成功能。
-
它是宣告相依性版本的集中位置,因此任何變更都會套用到每個子專案。
步驟 2. 瞭解專案相依性
Gradle 提供絕佳的相依性管理和自動化支援。
讓我們再看看我們的建置指令碼 (build.gradle(.kts)
檔案),特別是下列區段
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation(libs.guava)
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// This dependency is used by the application.
implementation libs.guava
}
Gradle 相依性管理中的一些關鍵概念包括
儲存庫 - 相依性的來源 → mavenCentral()
Maven Central 是 Maven 社群提供的 jar 檔案、外掛和函式庫的集合,並由 Sonatype 支援。它是 Java 的實際公開人工製品儲存庫,並由許多建置系統使用。
相依性 - 透過組態類型宣告的相依性 → libs.junit.jupiter
和 libs.guava
Gradle 需要特定資訊才能找到相依性。我們來看看 libs.guava
→ com.google.guava:guava:32.1.2-jre
和 libs.junit.jupiter
→ org.junit.jupiter:junit-jupiter-api:5.9.1
;它們分解如下
說明 | com.google.guava:guava:32.1.2-jre | org.junit.jupiter:junit-jupiter-api:5.9.1 | |
---|---|---|---|
群組 |
組織的識別碼 |
|
|
名稱 |
相依性識別碼 |
|
|
版本 |
要匯入的版本號 |
|
|
步驟 3. 了解傳遞相依性
傳遞相依性是相依性的相依性。
我們的 guava
相依性要運作,需要一個名為 failureaccess
的函式庫。因此 failureaccess
是專案的傳遞相依性。
步驟 4. 查看專案相依性
你可以使用 ./gradlew :app:dependencies
指令在終端機中查看你的相依性樹狀結構
$ ./gradlew :app:dependencies
> Task :app:dependencies
------------------------------------------------------------
Project ':app'
------------------------------------------------------------
...
compileClasspath - Compile classpath for source set 'main'.
\--- com.google.guava:guava:32.1.2-jre
+--- com.google.guava:guava-parent:32.1.2-jre
| +--- com.google.code.findbugs:jsr305:3.0.2 (c)
| +--- org.checkerframework:checker-qual:3.33.0 (c)
| +--- com.google.errorprone:error_prone_annotations:2.18.0 (c)
| \--- com.google.j2objc:j2objc-annotations:2.8 (c)
+--- com.google.guava:failureaccess:1.0.1
+--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
+--- com.google.code.findbugs:jsr305 -> 3.0.2
+--- org.checkerframework:checker-qual -> 3.33.0
+--- com.google.errorprone:error_prone_annotations -> 2.18.0
\--- com.google.j2objc:j2objc-annotations -> 2.8
輸出清楚地描述 com.google.guava:guava:32.1.2-jre
相依於 com.google.guava:failureaccess:1.0.1
。
步驟 5. 在 Build Scan® 中查看相依性
若要使用 Build Scan 查看相依性,請使用選用的 --scan
標記執行建置工作。
在 tutorial
目錄中,輸入以下指令並按照提示接受條款
$ ./gradlew build --scan
BUILD SUCCESSFUL in 423ms
7 actionable tasks: 7 up-to-date
Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes
Gradle Terms of Service accepted.
Publishing build scan...
https://gradle.com/s/link
Build Scan 是建置的可分享且集中記錄,Gradle 提供這項服務,而且免費。
按一下提示中提供的連結:https://gradle.com/s/link
。
你必須接受服務條款才能使用 Build Scan。 |
你必須使用你的電子郵件啟用 Build Scan

你會在收件匣中收到掃描的最後連結,如下所示

開啟選單中的相依性標籤,然後展開 compileClasspath
、runtimeClasspath
、testCompileClasspath
和 testRuntimeClasspath

正如預期的那樣,我們可以看到宣告的相依性 junit
和 guava
由 Gradle 用於編譯、執行和測試應用程式。
在視窗中展開 com.google.guava:guava:32.1.2-jre
和 org.junit.jupiter:junit-jupiter:5.9.1

junit
和 guava
下有幾個傳遞依賴項。例如,com.google.code.findbugs:jsr305:3.0.2
傳遞依賴項來自 com.google.guava:guava:32.1.2-jre
依賴項。
步驟 6. 更新專案依賴項
在建置檔案中新增和變更依賴項。
讓我們變更 guava
版本,並觀察這會如何影響依賴項樹狀結構。
將版本目錄中的 guava
依賴項變更為
[versions]
guava = "30.0-jre"
junit-jupiter = "5.10.0"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
如果您使用 IntelliJ 變更檔案,請記得按一下 sync
Gradle 按鈕

執行 ./gradlew build --scan
並檢視 Build Scan 結果

在終端機中執行 ./gradlew :app:dependencies
以查看依賴項樹狀結構中的變更
...
compileClasspath - Compile classpath for source set 'main'.
\--- com.google.guava:guava:30.0-jre
+--- com.google.guava:failureaccess:1.0.1
+--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
+--- com.google.code.findbugs:jsr305:3.0.2
+--- org.checkerframework:checker-qual:3.5.0
+--- com.google.errorprone:error_prone_annotations:2.3.4
\--- com.google.j2objc:j2objc-annotations:1.3
...
很明顯地,guava
依賴項已更新為版本 30.0
,而傳遞依賴項也已變更。
步驟 7. 執行 Java 應用程式
最後,使用 run
工作在您的終端機或 IDE 中,確認一切運作正常
./gradlew run > Task :app:compileJava UP-TO-DATE > Task :app:processResources NO-SOURCE > Task :app:classes UP-TO-DATE > Task :app:run Hello World!
下一步: 套用外掛程式 >>