解决gradle插件依赖冲突

问题描述 投票:3回答:1

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

The Situation

  1. 我有一个使用Gradle 4.x编译的Java项目。
  2. 该项目依赖于两个插件:gradle-jaxb-pluginserenity-gradle-plugin
  3. 两个插件共享一个依赖项,guice

The Problem

我需要升级其中一个插件(serenety)。升级会在调用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)
...

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

The Question

如何将插件依赖项彼此分开?我想包括两个插件,但看起来gradle将采用最高的依赖集并在任何地方使用它。

The Code

以下是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个回答
3
投票

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

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

当平静使用guice:4.0时,guice:4.0guice-assistedinject:3.0之间存在版本不匹配

解决方案是将guice依赖性排除在宁静之外,从而回归guice:3.0

Updated Code

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'
        }
    }
}
...

Alternative Solution

另一种可能性可能是需要guice-assistedinject:4.0,但上述工作因此我没有继续探索。

© www.soinside.com 2019 - 2024. All rights reserved.