简体   繁体   English

解决gradle插件依赖冲突

[英]Resolving gradle plugin dependency conflicts

TL;DR Two gradle plugins use different versions of the same dependency, resulting in compile errors when one of the plugins is invoked. TL; DR两个gradle插件使用相同依赖项的不同版本,在调用其中一个插件时导致编译错误。

The Situation 情况

  1. I have a Java project compiled using Gradle 4.x. 我有一个使用Gradle 4.x编译的Java项目。

  2. The project relies on two plugins: gradle-jaxb-plugin and serenity-gradle-plugin . 该项目依赖于两个插件: gradle-jaxb-pluginserenity-gradle-plugin

  3. Both plugins share a dependency, guice . 两个插件共享一个依赖, guice

The Problem 问题

I need to upgrade one of the plugins (serenety). 我需要升级其中一个插件(serenety)。 The upgrade results in a conflict at the point in which the jaxb plugin is invoked. 升级会在调用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)
...

I did some sleuthing and googling, and am fairly sure that the issue is rooted in the fact that the version of the serenity plugin uses guice 4.x when it used to use guice 3.x. 我做了一些调查和谷歌搜索,并且相当肯定这个问题的根源在于,当它曾经使用guice 3.x时,serenity插件的版本使用guice 4.x. The jaxb plugin uses guice 3.x. jaxb插件使用guice 3.x.

The Question 问题

How do I separate the plugin dependencies from one another? 如何将插件依赖项彼此分开? I would like to include both plugins, but it appears that gradle will take the highest dependency set and use that everywhere. 我想包括两个插件,但看起来gradle将采用最高的依赖集并在任何地方使用它。

The Code 代码

Here are the relevant portions of my build.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'
    ...
}

Note: You can replicate the issue by adding the serenity 1.5.1 plugin to the classpath dependencies block of the jaxb plugin examples 注意:您可以通过将serenity 1.5.1插件添加到jaxb插件示例的类路径依赖项块来复制该问题

TL;DR: When Gradle plugins share a dependency but use different versions of that dependency only the highest version is actually used. TL; DR:当Gradle插件共享依赖项但使用该依赖项的不同版本时,实际上只使用最高版本。 You have to explicitly exclude the higher-dependency version. 您必须明确排除较高依赖性版本。

The conflict here came because the jaxb plugin depends on guice:3.0 AND guice-assistedinject:3.0 . 这里的冲突是因为jaxb插件依赖于guice:3.0guice-assistedinject:3.0

When serenity uses guice:4.0 there was a version mismatch between guice:4.0 and guice-assistedinject:3.0 当宁静使用guice:4.0guice:4.0guice-assistedinject:3.0之间存在版本不匹配

The solution is to exclude the guice dependency from serenity, therefore falling back on guice: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 替代方案

Another possibility may have been to require guice-assistedinject:4.0 , but the above worked so I didn't continue to explore. 另一种可能性是需要guice-assistedinject:4.0 ,但上面的工作因此我没有继续探索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM