Reuse property with version number when adding dependency in sbt

I have a project built using sbt 0.11. I am trying to create a simple user interface with Scala Swing, so first I need to add a scala -swing dependency in my build.sbt file:

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

But I have a ScalaVersion SettingKey definition:

 scalaVersion := "2.9.1-1" 

How can I refer to this property? If I try to use it as

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

The compiler complains that it found sbt.SettingKey [String], while String is expected. There are get(...) and evaluate(...) methods in SettingKey, but they require some parameter of the [Scope] parameter.

What is the easiest way to just reference this property?

+6
source share
4 answers

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

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

(This is my preferred formatting, it actually calls the apply method on scalaVersion so you can write it in several different ways, for example, scalaVersion("org.scala-lang" % "scala-swing" % _) .)

If you had several settings that you wanted to depend on at the same time, you should apply them to the tuple:

 foo <<= (scalaVersion, organization) { (sv, o) => o + " uses Scala " + sv } 
+6
source
 libraryDependencies <+= scalaVersion("org.scala-lang" % "scala-swing" % _) 

< tells SBT that your parameter is dependent on another parameter.

+ tells SBT that you want to add a different value, rather than replacing existing ones (also indicates that the contents of this parameter are a sequence, and you add one element to it).

The syntax for setting(function) same as function(setting) , where function takes a parameter that is evaluated in the corresponding context as a parameter. I don’t even know how to write this, and it will be very much, so the shortcut is very useful.

You can also use (setting 1, setting 2)((a, b) => ... ) to create dependencies on several parameters.

PS: The following may work: it is a bit shorter, but it was deprecated without special compiler flags from 2.10.0.

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

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

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

Sort of

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

must work.

0
source

Source: https://habr.com/ru/post/914641/


All Articles