【问题标题】:Resolving gradle plugin dependency conflicts解决 gradle 插件依赖冲突
【发布时间】:2018-03-04 22:15:09
【问题描述】:

TL;DR 两个 gradle 插件使用相同依赖项的不同版本,导致调用其中一个插件时出现编译错误。

情况

  1. 我有一个使用Gradle 4.x 编译的Java 项目。

  2. 该项目依赖于两个插件:gradle-jaxb-pluginserenity-gradle-plugin

  3. 两个插件共享一个依赖项guice

问题

我需要升级其中一个插件(宁静)。升级会导致调用 jaxb 插件时发生冲突。

...
Caused by: java.lang.NoClassDefFoundError: com/google/inject/internal/util/$Maps
        at com.google.inject.assistedinject.BindingCollector.<init>(BindingCollector.java:34)
        at com.google.inject.assistedinject.FactoryModuleBuilder.<init>(FactoryModuleBuilder.java:206)
        at org.openrepose.gradle.plugins.jaxb.schema.guice.DocSlurperModule.configure(DocSlurperModule.groovy:43)
...

我做了一些调查和谷歌搜索,并且相当确定问题的根源在于 serenity 插件的版本在使用 guice 3.x 时使用的是 guice 4.x。 jaxb 插件使用 guice 3.x。

问题

如何将插件依赖项相互分离?我想同时包含这两个插件,但似乎 gradle 将采用最高依赖集并在任何地方使用它。

代码

这是我的 build.gradle 的相关部分

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.4.1'
        classpath 'net.serenity-bdd:serenity-gradle-plugin:1.5.1'
    }
}
...
project(':integration-tests') {
    apply plugin: 'java'
    apply plugin: 'net.serenity-bdd.aggregator'
    ...
}
...
project(':cms-business-model') {
    apply plugin: 'org.openrepose.gradle.plugins.jaxb'
    apply plugin: 'java'
    ...
}

注意:您可以通过将 serenity 1.5.1 插件添加到 the jaxb plugin examples 的类路径依赖项块来复制问题

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    TL;DR: 当 Gradle 插件共享一个依赖项但使用该依赖项的不同版本时,仅实际使用最高版本。您必须明确排除更高依赖的版本。

    这里的冲突是因为 jaxb 插件依赖于guice:3.0 AND guice-assistedinject:3.0

    当 serenity 使用 guice:4.0 时,guice:4.0guice-assistedinject:3.0 之间的版本不匹配

    解决方案是从 serenity 中排除 guice 依赖,因此退回到guice:3.0

    更新代码

    buildscript {
        repositories {
            mavenCentral()
            maven { url 'https://plugins.gradle.org/m2/' }
        }
        dependencies {
            classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.4.1'
            classpath ('net.serenity-bdd:serenity-gradle-plugin:1.5.1') {
                exclude group: 'com.google.inject', module:'guice'
            }
        }
    }
    ...
    

    替代解决方案

    另一种可能是要求guice-assistedinject:4.0,但上述方法有效,所以我没有继续探索。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 2023-01-13
      • 2014-09-10
      相关资源
      最近更新 更多