38

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?

Community
  • 1
  • 1
Paul Draper
  • 78,542
  • 46
  • 206
  • 285

1 Answers1

69

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"
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
  • 2
    don't forget about this tine remark at the end ```Note: this is an Ivy-only feature and will not be included in a published pom.xml.``` – Eugene Platonov Dec 08 '16 at 21:47
  • Try `libraryDependencies := Set()` – Leszek Gruchała Mar 16 '17 at 15:21
  • 4
    `dependencyOverrides ++= Set(... foo ...)` – Leo Romanovsky May 05 '17 at 04:05
  • If that's not working for anybody, try adding dependencyOverrides += "...foo.. " much like in this manner: https://stackoverflow.com/a/37966356/7082628 – NateH06 Mar 12 '18 at 01:47
  • @EugenePlatonov, and that's okay. It would be confusing with multiple overrides; I just need an escape hatch. – Paul Draper Sep 19 '20 at 22:42
  • Where should we add dependencyOverrides? After the library dependencies or before them one we want to override version of one of transitive dependency coming from another dependency. This is relatd to log4j issue on scala spark projects. – Sangeeta Jun 09 '22 at 08:14