+-
java-解决gradle插件依赖冲突

TL;DR Two gradle plugins use different versions of the same dependency, resulting in compile errors when one of the plugins is invoked.

情况

>我有一个使用Gradle 4.x编译的Java项目.
>该项目依赖于两个插件:gradle-jaxb-plugin和serenity-gradle-plugin.
>两个插件都共享一个依赖项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时,它使用的是guice4.x. jaxb插件使用guice3.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的classpath依赖关系块中来复制问题.

最佳答案

TL;DR: When Gradle plugins share a dependency but use different versions of that dependency only the highest version is actually used. You have to explicitly exclude the higher-dependency version.

之所以发生冲突,是因为jaxb插件依赖guice:3.0和guice-assistedinject:3.0.

当宁静使用guice:4.0时,guice:4.0和guice-assistedinject:3.0之间版本不匹配

解决方案是将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,但以上方法有效,因此我没有继续进行探索.

点击查看更多相关文章

转载注明原文:java-解决gradle插件依赖冲突 - 乐贴网