java Gradle 中的多个版本的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29374885/
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-11-02 15:08:26  来源:igfitidea点击:

Multiple version of dependencies in Gradle

javagradle

提问by bjar-bjar

i'm building a java project, using gradle for version control.

我正在构建一个 java 项目,使用 gradle 进行版本控制。

I'm migrating from an old version of the Drools rules engine 5.5.0 to 6.2.0. Instead of going 'big bang' and change everey class to use the new version, I would like to change one class at the time, and remove the old dependency when all the classes are migrated.

我正在从旧版本的 Drools 规则引擎 5.5.0 迁移到 6.2.0。我不想“大爆炸”并更改每个类以使用新版本,而是想在当时更改一个类,并在迁移所有类时删除旧的依赖项。

In my gradle.build I have set:

在我的 gradle.build 中,我设置了:

compile 'org.drools:drools-compiler:6.2.0.Final'
compile 'org.kie:kie-api:6.2.0.Final'  
compile 'org.drools:drools-core:6.2.0.Final'

compile 'org.drools:drools-core:5.5.0.Final'
compile 'org.drools:drools-compiler:5.5.0.Final'

But it only downloads the newest version of the libraries. Does gradle support multiple versions of the same library?

但它只下载最新版本的库。gradle 是否支持同一个库的多个版本?

回答by keke2048

To download multiple version of the same library:

下载同一个库的多个版本:

repositories {
  mavenCentral()
}
  configurations {
  compile5
  compile6
}
  dependencies {
  compile5 'org.osgi:org.osgi.core:5.0.0'
  compile6 'org.osgi:org.osgi.core:6.0.0'
}
  task libs(type: Sync) {
  from configurations.compile5
  from configurations.compile6
  into "$buildDir/libs"
}

refer to: How to get multiple versions of the same library

参考:如何获取同一个库的多个版本

By the way

顺便一提

  • above download two versions, while compiling with only one version
  • 上面下载两个版本,编译时只有一个版本

Gradle offers the following conflict resolution strategies:

Newest: The newest version of the dependency is used. This is Gradle's default strategy, and is often an appropriate choice as long as versions are backwards-compatible.

Fail: A version conflict results in a build failure. This strategy requires all version conflicts to be resolved explicitly in the build script. See ResolutionStrategyfor details on how to explicitly choose a particular version.

Gradle 提供了以下冲突解决策略:

Newest:使用最新版本的依赖项。这是 Gradle 的默认策略,只要版本向后兼容,通常是合适的选择。

失败:版本冲突导致构建失败。此策略要求在构建脚本中明确解决所有版本冲突。有关如何明确选择特定版本的详细信息,请参阅ResolutionStrategy

refer to: 23.2.3. Resolve version conflicts of Chapter 23

参考:23.2.3。解决第23章的版本冲突

回答by mushfek0001

No gradle doesn't support multiple version of the same library. It will choose the newest by default but you can change this using

没有 gradle 不支持同一个库的多个版本。默认情况下它会选择最新的,但您可以使用

org.gradle.api.artifacts.ResolutionStrategy.failOnVersionConflict()

In case of conflict, Gradle by default uses the newest of conflicting versions. However, you can change this behavior. Use this method to configure the resolution to fail eagerly on any version conflict, e.g. multiple different versions of the same dependency (group and name are equal) in the same Configuration.

如果发生冲突,Gradle 默认使用最新的冲突版本。但是,您可以更改此行为。使用此方法将解析配置为在任何版本冲突时急切失败,例如同一配置中相同依赖项(组和名称相等)的多个不同版本。

Taken from here https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

取自这里https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html