Java 如何强制 Gradle 为两个依赖项设置相同的版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28444016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:14:07  来源:igfitidea点击:

How can I force Gradle to set the same version for two dependencies?

javagradledependencies

提问by confile

I use the following two dependencies:

我使用以下两个依赖项:

compile 'com.google.guava:guava:14.0.1'
compile 'com.google.guava:guava-gwt:14.0.1'

Both must be the same version to work correctly. Since my other dependencies use a higher version, Gradle uses different versions for each dependency.

两者必须是相同的版本才能正常工作。由于我的其他依赖项使用更高版本,因此 Gradle 为每个依赖项使用不同的版本。

I found this by running gradle dependencies:

我通过运行找到了这个gradle dependencies

compile - Compile classpath for source set 'main'.
+--- com.google.guava:guava:14.0.1 -> 17.0
+--- com.google.guava:guava-gwt:14.0.1
|    +--- com.google.code.findbugs:jsr305:1.3.9
|    \--- com.google.guava:guava:14.0.1 -> 17.0 

How can I force Gradle to set the same version for these two dependencies?

如何强制 Gradle 为这两个依赖项设置相同的版本?

采纳答案by Klunk

One of your dependencies is forcing the guava version to update. Use gradle dependenciesto locate which library is evicting your version.

您的依赖项之一是强制更新番石榴版本。使用gradle dependencies以定位库驱逐你的版本。

The problem you have is that if you force it to use 14.0.1 another library may not work properly. Can you not just use the 17.0 version as your dependency?

您遇到的问题是,如果您强制它使用 14.0.1,另一个库可能无法正常工作。你不能只使用 17.0 版本作为你的依赖吗?

Rather than maintain individual version numbers in the build.gradle I use a dependencies.gradle file which will have a mapping of version numbers and pull that into the build.gradle. That way I only need to maintain the single guava version. So your example will be:

我没有在 build.gradle 中维护单独的版本号,而是使用一个 dependencies.gradle 文件,该文件将具有版本号的映射并将其拉入 build.gradle。这样我只需要维护单个番石榴版本。所以你的例子是:

dependencies.gradle

依赖项.gradle

ext {
    ver = [
        guava: '14.0.1'
    ]
}

and then in the build.gradle file you can have:

然后在 build.gradle 文件中,您可以拥有:

apply from: "dependencies.gradle"

dependencies {
    compile group: 'com.google.guava', module: 'guava', version: ver.guava
    compile group: 'com.google.guava', module: 'guava-gwt', version: ver.guava
}

then when I want to move to 17.0 I only need to change the dependencies.gradle.

然后当我想移动到 17.0 时,我只需要更改dependencies.gradle。

I am also a definite fan of setting transitive dependencies to false with

我也是将传递依赖设置为 false 的忠实粉丝

configurations.compile { transitive = false }

this way you do not have some dependencies evicted at compile time, although you may have a problem at run time if the evicting library is not fully backward compatible. Lets face it if you are writing the code you should know what libraries you use and you should be explicit about your dependencies. It protects you from one of your dependencies upgrading and messing you up.

这样您就不会在编译时驱逐某些依赖项,尽管如果驱逐库不完全向后兼容,您可能会在运行时遇到问题。让我们面对现实吧,如果您正在编写代码,您应该知道您使用了哪些库,并且您应该明确说明您的依赖项。它可以保护您免于升级和弄乱您的依赖项之一。

回答by cmcginty

Add this section to dependencies.gradle file

将此部分添加到 dependencies.gradle 文件

configurations.all {
        resolutionStrategy { 
            force 'com.google.guava:guava:14.0.1'
            force 'com.google.guava:guava-gwt:14.0.1'
        }
    }

回答by Pijusn

I had a similar situation where one of the dependencies used spring-web 4.2.4 which was broken. You have to force specific library version you want. As mentioned in another comment, it might cause compatibility issues but sometimes is necessary.

我遇到过类似的情况,其中一个依赖项使用了已损坏的 spring-web 4.2.4。您必须强制使用所需的特定库版本。正如另一条评论中提到的,它可能会导致兼容性问题,但有时是必要的。

Least intrusive way of forcing a library version I found was instead of using

我发现的强制库版本的最少侵入方式是而不是使用

compile "org.springframework:spring-web:4.2.3.RELEASE"

specifying dependency configuration as forced:

强制指定依赖项配置:

compile("org.springframework:spring-web:4.2.3.RELEASE"){
    force = true
}

I used it when I needed to downgrade Spring version temporarily (until next release).

我在需要暂时降级 Spring 版本时使用它(直到下一个版本)。

回答by Vyacheslav Shvets

configurations.all {
  resolutionStrategy {  
    eachDependency { DependencyResolveDetails details ->
      if (details.requested.group == 'com.google.guava') {
        details.useVersion "14.0.1"
      }
    }
  }
}

dependencies {
  compile 'com.google.guava:guava'
  compile 'com.google.guava:guava-gwt'
}

回答by Stevo Slavi?

Alternatively you can use dependencySets(or mavenBomwhen BOM POM is available) support in spring-dependency-managementGradle plugin. Note that this plugin is also automatically applied with spring-bootGradle plugin. For more details see here.

或者,您可以在spring-dependency-managementGradle 插件中使用依赖集(或mavenBom,当 BOM POM 可用时)支持。请注意,此插件也会自动与spring-bootGradle 插件一起应用。有关更多详细信息,请参阅此处

plugins {
  id 'io.spring.dependency-management' version '1.0.1.RELEASE'
}

dependencyManagement {
  dependencies {
    dependencySet(group: 'com.google.guava', version: '14.0.1') {
      entry 'guava'
      entry 'guava-gwt'
    }
  }
}

dependencies {
  compile 'com.google.guava:guava'
  compile 'com.google.guava:guava-gwt'
}

回答by Vaiden

I would suggest against setting transitive = false, as this approach would force yo to resolve the dependency tree yourself, manually.

我建议不要设置transitive = false,因为这种方法会迫使你自己手动解析依赖树。

You could either force the desired guava version via configurations.all, or add the dependency explicitly and set it forced = true.

您可以通过 强制所需的番石榴版本configurations.all,或者显式添加依赖项并设置它forced = true

Examples here: http://www.devsbedevin.net/android-understanding-gradle-dependencies-and-resolving-conflicts/

这里的例子:http: //www.devsbedevin.net/android-understanding-gradle-dependencies-and-resolving-conflicts/

回答by Marcin Koziński

If it's OK to just use the newer version for both dependencies, the simplest way to fix your problem is to update your dependencies:

如果对两个依赖项都使用较新版本就可以了,那么解决问题的最简单方法是更新您的依赖项:

compile 'com.google.guava:guava:17.0'
compile 'com.google.guava:guava-gwt:17.0'

That will make sure both of them are on 17.0. It's simpler than trying to force both of them on the older version and as an added bonus you get a newer version, which (probably) comes with bug fixes and new features.

这将确保他们都在 17.0.0 上。这比试图在旧版本上强制使用它们更简单,并且作为额外的好处,您可以获得一个新版本,它(可能)带有错误修复和新功能。

To be fair @Klunk mentions this in his answer, by asking "Can you not just use the 17.0 version as your dependency?", but it's just in passing and easy to miss, so I thought it made sense to post as a separate answer.

公平地说,@Klunk 在他的回答中提到了这一点,他问“你不能只使用 17.0 版本作为你的依赖吗?”,但它只是顺便说一句,很容易错过,所以我认为作为单独的答案发布是有意义的.

回答by sendon1982

Another option is to use dependency constraint: https://docs.gradle.org/current/userguide/dependency_constraints.html

另一种选择是使用依赖约束:https: //docs.gradle.org/current/userguide/dependency_constraints.html

dependencies {
    implementation 'org.apache.httpcomponents:httpclient'
    constraints {
        implementation('org.apache.httpcomponents:httpclient:4.5.3') {
            because 'previous versions have a bug impacting this application'
        }
        implementation('commons-codec:commons-codec:1.11') {
            because 'version 1.9 pulled from httpclient has bugs affecting this application'
        }
    }
}