gradle 强制版本的传递依赖不起作用。似乎不适用排除、覆盖或强制

Geo*_*ler 6 gradle transitive-dependency dependency-resolution

我与传递依赖有冲突。推翻、排除或强迫都无济于事。我还能做什么才能将正确版本的库放入 jar 中?完整的代码是。找到了https://github.com/geoHeil/gradle-dependency-resolution,但其主要部分如下所述。

问题描述

执行

./gradlew shadowJar
Run Code Online (Sandbox Code Playgroud)

禁用 geomesa 依赖项(引入过时版本的 typesafe/lightbend 配置库):

dependencies {

    compile "com.github.kxbmap:configs_2.11:0.4.4"
    //compile "org.locationtech.geomesa:geomesa-hbase-spark-runtime_2.11:2.0.1"
}
Run Code Online (Sandbox Code Playgroud)

并执行 jar

java -jar build/libs/gradleThing-all.jar                         
Run Code Online (Sandbox Code Playgroud)

输出

hello
my config is: Success(Job1Configuration(frequencyCounting))
Run Code Online (Sandbox Code Playgroud)

启用依赖项时:

./gradlew shadowJar
java -jar build/libs/gradleThing-all.jar                         
Run Code Online (Sandbox Code Playgroud)

它失败了

hello
Exception in thread "main" java.lang.Exception: Failed to start. There is a problem with the configuration: Vector([extract] com.typesafe.config.Config.hasPathOrNull(Ljava/lang/String;)Z)
Run Code Online (Sandbox Code Playgroud)

这与配置库的过时版本有关,如何解决类型安全配置上的NoSuchMethodError?。还通过以下方式确认:

gradle dependencyInsight --dependency om.typesafe:config

> Task :dependencyInsight
com.typesafe:config:1.3.1 (conflict resolution)
   variant "runtime" [
      Requested attributes not found in the selected variant:
         org.gradle.usage = java-api
   ]
\--- com.github.kxbmap:configs_2.11:0.4.4
     \--- compileClasspath

com.typesafe:config:1.2.1 -> 1.3.1
   variant "runtime" [
      Requested attributes not found in the selected variant:
         org.gradle.usage = java-api
   ]
+--- org.locationtech.geomesa:geomesa-convert-avro_2.11:2.0.1
|    \--- org.locationtech.geomesa:geomesa-convert-all_2.11:2.0.1
|         +--- org.locationtech.geomesa:geomesa-tools_2.11:2.0.1
|         |    \--- org.locationtech.geomesa:geomesa-hbase-spark-runtime_2.11:2.0.1
|         |         \--- compileClasspath
...
Run Code Online (Sandbox Code Playgroud)

如何修复对我想要的 1.3.3 版本的传递依赖?

我的解决方案

尝试设置:

compile("org.locationtech.geomesa:geomesa-hbase-spark-runtime_2.11:2.0.1") {
                exclude group: 'com.typesafe', module: 'config'
            }
Run Code Online (Sandbox Code Playgroud)

并重新运行 jar 再次失败并出现同样的问题。

还有一个约束https://docs.gradle.org/current/userguide/managing_transitive_dependency.html#sec:dependency_constraints,例如:

implementation("com.typesafe:config")
    constraints {
        implementation("com.typesafe:config:1.3.3") {
            because 'previous versions miss a method /sf/ask/2842757151/'
        }
    }
Run Code Online (Sandbox Code Playgroud)

或使用强制https://docs.gradle.org/current/userguide/managing_transitive_dependency.html#sec:enforcing_dependency_version,例如:

implementation('com.typesafe:config:1.3.3') {
        force = true
    }
Run Code Online (Sandbox Code Playgroud)

或类似:

configurations.all {
    resolutionStrategy {
        force 'com.typesafe:config:1.3.3'
    }
}
Run Code Online (Sandbox Code Playgroud)

没有给出正确的版本。

结果

所有变体均因相同错误而失败

怎么了?必须有一种方法来强制使用所需的版本。

Lou*_*met 2

您遇到的问题与您所描述的完全相反。

您在项目中尝试的不同操作都会确保 的版本com.typesafe:config1.3.x.

但是,在添加时,org.locationtech.geomesa:geomesa-hbase-spark-runtime_2.11:2.0.1您会引入一些已经具有来自com.typesafe:config但来自该1.2行的类的依赖项。当您创建 fat jar 时,该版本会覆盖该1.3行中的类。

这可以通过解压你的 fat jar 并运行来看到:

$ javap com/typesafe/config/Config.class | grep hasPath
  public abstract boolean hasPath(java.lang.String);
Run Code Online (Sandbox Code Playgroud)

表明确实hasPathOrNull缺少该方法。

https://issues.apache.org/jira/browse/SPARK-9441中暗示了该阴影问题。

鉴于此,简单的路径(如果可能的话)是降级"com.github.kxbmap:configs_2.11到依赖于com.typesafe:config:1.2.x

另一个解决方案是准确找出哪个 fat jar 包含这些内容,看看是否可以将其从您自己的 fat jar 中排除。