为什么我的库的编译依赖项会泄漏?

  • 本文关键字:泄漏 依赖 编译 java gradle
  • 更新时间 :
  • 英文 :


我正在使用Gradle 3.4.1。

我有一个使用 Gradle java 库插件构建的库(我们称之为utils),它会产生一个不错的 JAR 文件。这是依赖项部分:

dependencies {
// public API
api group: "org.postgresql", name: "postgresql", version: "9.2-1004-jdbc4"    
api group: "log4j", name: "log4j", version: "1.2.17"
// implementation specific
implementation group: "commons-configuration", name: "commons-configuration" , version: "1.10"
implementation group: "commons-lang", name: "commons-lang" , version: "2.6"
}

现在我的项目包括这个库以及其他一些Apache Commons库:

dependencies {
compile group: "com.foo", name: "utils", version: "6.+", changing: true
compile group: "org.apache.commons", name: "commons-lang3", version: "3.5"
compile group: "commons-io", name: "commons-io" , version: "2.5"
}

在项目的类路径中,我现在有commons-lang库和commons-lang3库,尽管我指定了适当的依赖项作为implementation!根据文档 https://docs.gradle.org/3.4.1/userguide/java_library_plugin.html 这应该是正确的方法。

这是我项目的依赖项列表:

default - Configuration for default artifacts.
+--- com.foo:utils:6.+ -> 6.0.0
|    +--- org.postgresql:postgresql:9.2-1004-jdbc4
|    +--- log4j:log4j:1.2.17
|    +--- commons-configuration:commons-configuration:1.10
|    |    +--- commons-lang:commons-lang:2.6
|    |    --- commons-logging:commons-logging:1.1.1
|    --- commons-lang:commons-lang:2.6
+--- org.apache.commons:commons-lang3:3.5
--- commons-io:commons-io:2.5

我做错了什么?如何摆脱外部依赖commons-langcommons-configuration

有几种方法可以排除传递依赖项,在几个问题和 gradle 文档中都有介绍。

configurations {
compile.transitive = false
}

configurations.all {
exclude group:"ch.qos.logback", module:"logback-core"
}

dependencies {
compile ('foo:bar:0.1') {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
}

最新更新