删除 gradle 中的传递类路径依赖

Remove transitive classpath dependency in gradle

我们是 运行 一个 spring-boot 应用 gradle。

为了包含 spring-boot 插件,我们将其添加为依赖项:

buildscript {
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE") 
    }
}

不幸的是,这个插件依赖于 org.apache.logging.log4j:log4j-slf4j-impl:2.4.1

我想排除的。

已经尝试添加:

  dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE") {
            exclude group: 'org.apache.logging.log4j'
        }
    }

这行不通。

同时添加:

configurations {
    classpath.exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
}

没有任何影响。

欢迎任何提示。

如果您要排除

org.apache.logging.log4j:log4j-slf4j-impl:2.4.1

尝试

dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE") {
        exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
    }
}

当我想确保一个依赖项从未添加到项目中时,我通常会回退到这个。

configurations {
    all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl', version '2.4.1'
}

但是你确定你需要排除依赖吗?它不应该是构建的一部分 jar/war。 您可以使用 "gradlew dependencies".

检查所有依赖项

问题是我将 log4j 声明为运行时依赖项:

  ext {
       log4jVersion="2.5"
  }
  runtime (
            "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion",
            "org.apache.logging.log4j:log4j-api:$log4jVersion",
            "org.apache.logging.log4j:log4j-core:$log4jVersion"
        )

这导致版本 2.4.1 被某些编辑器魔术作为编译依赖项获取。

因此我在类路径上有 2.4.1 和 2.5。

我一声明 log4j 作为编译依赖项 2.4.1 就消失了...

两步,追查传递依赖,然后将其从负责的库中排除。

gradle dependencies 为您提供包括可传递项在内的完整列表。如果您的项目很小,那可能会有所帮助,但对于大型企业构建……它的信息太多了。随意搜索它,但我们从 dependencyInsight.

获得了更多重要信息

gradle dependencyInsight --dependency someDependency 找到依赖项可能进入构建的所有位置。如果您有多个版本,这将有助于明确版本的来源。

在我的用例中,日志记录被明确声明为编译时依赖项,因此如下所示。如果 log4j 在其他任何地方,您会看到有问题的库以及 v2.5.

的编译时声明

我必须在每个子模块上明确地运行这个。

$ gradle util:dependencyInsight --dependency org.apache.logging.log4j
Configuration on demand is an incubating feature.
:util:dependencyInsight
org.apache.logging.log4j:log4j-api:2.5
+--- compile
\--- org.apache.logging.log4j:log4j-core:2.5
     \--- compile

org.apache.logging.log4j:log4j-core:2.5
\--- compile

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 0.933 secs

现在,一旦您知道从何处排除依赖项,就可以像以前一样将其删除。您可以再次 运行 确认 dependencyInsights

dependencies {
    // found through `gradle dependencyInsight --dependency org.apache.logging.log4j`
    classpath("someOtherGroup:someOtherArtifactId:1.0") {
        exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
    }
}

另一个解决方案可能是覆盖依赖解析器并强制版本为 2.5

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == "org.apache.logging.log4j") {
            println "Updating version for: $details.requested.group:$details.requested.name:$details.requested.version --> 2.5"
            details.useVersion '2.5'
        }
    }
}

我的意见是,我可能不想一直在 resolutionStrategy 中添加检查,所以最好只在 dependencyInsights 中追踪它。这也意味着在两个地方更新版本,如果另一个开发人员不知道 gradle 的 resolutionStrategy 是如何工作的,那么他们将有 "weird" 行为......例如。我将 log4j 更新为 2.7 但它仍然使用 2.5?!?!

构建

但这两种方法都是有效的