Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 get(...) and evaluate(...) on SettingKey but they require some Setting[Scope] parameter to be passed in.

What is the simplest way to just reference this property?

like image 279
Dmitry Serdiuk Avatar asked May 01 '12 15:05

Dmitry Serdiuk


People also ask

Which is the correct way to add dependencies in sbt file?

Library dependencies can be added in two ways: unmanaged dependencies are jars dropped into the lib directory. managed dependencies are configured in the build definition and downloaded automatically from repositories.

How do we specify library dependencies in sbt?

You can use both managed and unmanaged dependencies in your SBT projects. If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.

How can I change sbt version?

In the Project tool window, in the source root directory, locate the build. properties file and open it in the editor. In the editor explicitly specify the version of sbt that you want to use in the project. The newer sbt versions(1.0 +) will create the build.


3 Answers

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 }
like image 101
RM. Avatar answered Sep 20 '22 11:09

RM.


libraryDependencies <+= scalaVersion("org.scala-lang" % "scala-swing" % _)

The < tells SBT that your setting has a dependency on another setting.

The + tells SBT that you want to add another value, not replace the existing ones (also, it indicates the the contents of the setting are a sequence, and you are adding one element to it).

The syntax setting(function) is the same as function(setting), where function takes a setting evaluated at the proper context as parameter. I don't even know how to write that, and it would be very verbose, so the shortcut is very helpful.

One can also use (setting 1, setting 2)((a, b) => ... ) to make dependencies on multiple settings.

PS: The following might works as well, and it is a bit shorter, but it has been deprecated without special compiler flags as of 2.10.0.

libraryDependencies <+= scalaVersion("org.scala-lang" % "scala-swing" %)
like image 20
Daniel C. Sobral Avatar answered Sep 23 '22 11:09

Daniel C. Sobral


Realising this is old - adding an answer in case anyone else comes across it. Just add .value to the scalaVersion variable to get the string value:

libraryDependencies += "org.scala-lang" % "scala-swing" % scalaVersion.value
like image 28
sagron Avatar answered Sep 19 '22 11:09

sagron