SBT dependency single and double per cent sign

Abstract: This article introduce how to use the single (%) and double (%%) per cent sign in sbt dependency.

Introduction

As we know, in the sbt dependency description, there are two type symbols:

  1. single per cent sign%
  2. double per cent sign%%

The latest Play framework official document Managing library dependencies give a detail description of how to add the dependency to the sbt build description file.

Getting the right Scala version with %%

If you use groupID %% artifactID % revision rather than groupID % artifactID % revision (the difference is the double %% after the groupID), sbt will add your project’s Scala version to the artifact name. This is just a shortcut. You could write this without the %%:

1
libraryDependencies += "org.scala-stm" % "scala-stm_2.11" % "0.8"

Assuming the scalaVersion for your build is 2.11.1, the following is identical (note the double %% after "org.scala-tools"):

1
libraryDependencies += "org.scala-stm" %% "scala-stm" % "0.8"

The idea is that many dependencies are compiled for multiple Scala versions, and you’d like to get the one that matches your project to ensure binary compatibility.

What’s the matter?

One important thing, most of the dependency from the maven central repository, you should use the single %, because they do not care about the Scala version. If you use the double %%, the sbt will expand it with Scala version, and it will not match the maven.

For example , you should use this:

1
libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.26"

if you use the double per cent sign, the final dependency will not be found because it will be changed to slf4j-api_[CURRENT_SCALA_VERSION]

If there is Scala column for the dependency in maven central repository which means that you can use double per cent sign, but you should still make sure that the Scala version you are using occurs in the list of the dependency.

For example, if you use Akka-actor version of 2.5.18, you can only use the scala 2.11 and 2.12