首頁  >  文章  >  Java  >  如何在 Gradle 中排除特定的傳遞依賴?

如何在 Gradle 中排除特定的傳遞依賴?

Susan Sarandon
Susan Sarandon原創
2024-10-26 10:44:03661瀏覽

How to Exclude Specific Transitive Dependencies in Gradle?

使用Gradle 排除傳遞依賴

在Gradle 中,使用應用程式外掛程式產生jar 檔案時,您可能會遇到傳遞依賴,您可能想要排除。為此,可以使用排除方法。

排除的預設行為

最初,嘗試排除org.slf4j:slf4j- 的所有實例log4j12 使用以下程式碼:

configurations {
  runtime.exclude group: "org.slf4j", name: "slf4j-log4j12"
}

但是但是,這導致排除所有

org.slf4j 工件,包括slf4j-api。

自訂排除

要細化排除,可以利用群組和模組屬性:
configurations {
  runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}

此方法成功僅排除org.slf4j :slf4j-log4j12,而不影響其他slf4j

從單一依賴項排除

如果需要排除特定依賴項,可以使用以下語法:
dependencies {
  compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
    exclude group: "org.slf4j", module: "slf4j-log4j12"
  }
}

排除方法的限制

需要注意的是,雖然可以在排除中指定任意屬性,但從單一依賴項中排除時不允許這樣做。例如,以下程式碼將失敗:
dependencies {
  compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
    exclude group: "org.slf4j", name: "slf4j-log4j12"
  }
}

並顯示以下錯誤訊息:
No such property: name for class: org.gradle.api.internal.artifacts.DefaultExcludeRule

了解Gradle 模組

在Gradle 中,模組屬性對應於Maven的artifactId。因此,若要確定特定 Maven 工件的模組,請檢查其artifactId。例如,Maven 工件 org.slf4j:slf4j-log4j12 將具有 slf4j-log4j12 的 Gradle 模組。

以上是如何在 Gradle 中排除特定的傳遞依賴?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn