SBT: How to control which version of a managed dependency is used

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.5, “SBT: How to control which version of a managed dependency is used.”

Problem

In a Scala project, you want to make sure you always have the desired version of a managed dependency in SBT, including the latest integration release, milestone release, or other versions.

Solution

The revision field in the libraryDependencies setting isn’t limited to specifying a single, fixed version. According to the Apache Ivy documentation, you can specify terms such as latest.integration, latest.milestone, and other terms.

As one example of this flexibility, rather than specifying version 1.8 of a foobar module, as shown here:

libraryDependencies += "org.foobar" %% "foobar" % "1.8"

you can request the latest.integration version like this:

libraryDependencies += "org.foobar" %% "foobar" % "latest.integration"

The module developer will often tell you what versions are available or should be used, and Ivy lets you specify tags to control which version of the module will be downloaded and used. The Ivy “dependency” documentation states that the following tags can be used:

  • latest.integration
  • latest.[any status], such as latest.milestone
  • You can end the revision with a + character. This selects the latest subrevision of the dependency module. For instance, if the dependency module exists in revisions 1.0.3, 1.0.7, and 1.1.2, specifying 1.0.+ as your dependency will result in 1.0.7 being selected.
  • You can use “version ranges,” as shown in the following examples:

    [1.0,2.0] matches all versions greater or equal to 1.0 and lower or equal to 2.0 [1.0,2.0[ matches all versions greater or equal to 1.0 and lower than 2.0 ]1.0,2.0] matches all versions greater than 1.0 and lower or equal to 2.0 ]1.0,2.0[ matches all versions greater than 1.0 and lower than 2.0 [1.0,) matches all versions greater or equal to 1.0 ]1.0,) matches all versions greater than 1.0 (,2.0] matches all versions lower or equal to 2.0 (,2.0[ matches all versions lower than 2.0

(These configuration examples are courtesy of the Apache Ivy documentation. See the link in the See Also section for more information.)

To demonstrate a few of these tags, this example shows the latest.milestone tag:

libraryDependencies += "org.scalatest" %% "scalatest" % "latest.milestone" % " "test"

At the time of this writing, it retrieves this file:

scalatest_2.10-2.0.M6-SNAP13.jar

This specification demonstrates the + tag:

libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.+" % "test"

It currently retrieves this file:

scalatest_2.10-1.9.2-SNAP1.jar

See Also