bjar-bjar
bjar-bjar

Reputation: 707

Multiple version of dependencies in Gradle

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

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.

In my gradle.build I have set:

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?

Upvotes: 20

Views: 47919

Answers (2)

keke2048
keke2048

Reputation: 346

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 ResolutionStrategy for details on how to explicitly choose a particular version.

refer to: 23.2.3. Resolve version conflicts of Chapter 23

Upvotes: 22

mushfek0001
mushfek0001

Reputation: 3935

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

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.

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

Upvotes: 8

Related Questions