Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a specific version of dependency?

Tags:

sbt

A dependency bar depends on foo 1.2.3, but that version of foo has a bug and I need to use version 1.2.2.

I can do that with force().

libraryDependencies += "foo" %% "foo" % "1.2.2" force()

That method is not recommended by the docs:

Forcing a revision (Not recommended)

Note: Forcing can create logical inconsistencies so it’s no longer recommended.

Does this mean SBT has a different, better way than force() to use a specific version of a dependency? If so, what?

Or am I to infer from the documentation that this entire problem is one that I'm recommended not to have?

like image 911
Paul Draper Avatar asked Apr 08 '16 13:04

Paul Draper


People also ask

How do I override the version of a transitive dependency?

How do you do this if the wrong dependency is a transitive dependency? By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

What is a dependency constraint?

Dependency constraints allow you to define the version or the version range of both dependencies declared in the build script and transitive dependencies. It is the preferred method to express constraints that should be applied to all dependencies of a configuration.

How do you override a dependency in Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.


1 Answers

you can use dependencyOverrides:

dependencyOverrides += "foo" %% "foo" % "1.2.2" 

You're not avoiding "logical inconsistencies" anyway. If you force a version, you have to manually take care of compatibility with other libraries, there's no way out of that.

From the documentation:

Overriding a version

For binary compatible conflicts, sbt provides dependency overrides. They are configured with the dependencyOverrides setting, which is a set of ModuleIDs. For example, the following dependency definitions conflict because spark uses log4j 1.2.16 and scalaxb uses log4j 1.2.17:

libraryDependencies ++= Seq(   "org.spark-project" %% "spark-core" % "0.5.1",       "org.scalaxb" %% "scalaxb" % "1.0.0" )  

The default conflict manager chooses the latest revision of log4j, 1.2.17:

show update  [info] compile:  [info]    log4j:log4j:1.2.17: ... ...  [info]    (EVICTED) log4j:log4j:1.2.16 ...  

To change the version selected, add an override:

dependencyOverrides += "log4j" % "log4j" % "1.2.16" 
like image 183
Giovanni Caporaletti Avatar answered Sep 16 '22 23:09

Giovanni Caporaletti