Dmitry Serdiuk Dmitry Serdiuk - 10 months ago 45
Scala Question

Reuse property with version number when adding a dependency in sbt

I have a project built with sbt 0.11.
I'm trying to create a simple UI with Scala Swing, so first thing is to add a dependency on scala-swing in my build.sbt:

libraryDependencies += "org.scala-lang" % "scala-swing" % "2.9.1-1"

But I have a SettingKey scalaVersion defined:

scalaVersion := "2.9.1-1"

How can I reference that property? If I try to use it like

libraryDependencies += "org.scala-lang" % "scala-swing" % scalaVersion

Compiler complains that it found sbt.SettingKey[String] while String is expected. There are methods
on SettingKey but they require some Setting[Scope] parameter to be passed in.

What is the simplest way to just reference this property?

Answer Source

You need to tell the system that libraryDependencies now depends on scalaVersion:

libraryDependencies <+= (scalaVersion) { sv => "org.scala-lang" % "scala-swing" % sv }

(that's my preferred formatting; it's actually invoking the apply method on scalaVersion so you could write it a few different ways, e.g., scalaVersion("org.scala-lang" % "scala-swing" % _).)

If you had multiple settings you wanted to depend on simultaneously, you'd apply on the tuple of them:

foo <<= (scalaVersion, organization) { (sv, o) => o + " uses Scala " + sv }