Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT cross building - choosing a different library version for different scala version

Tags:

scala

sbt

Is it possible to configure SBT to use a completely different library version when cross building, depending on the scala version the project is being built with?

For example:

  • When building with Scala 2.9.2 I want to use "org.scalatest" % "scalatest_2.9.2" % "2.0.M5" % "test"
  • When building with scala 2.10.0 I want to use "org.scalatest" % "scalatest_2.10.0-RC5" % "2.0.M5-B1" % "test"
like image 886
theon Avatar asked Dec 21 '12 00:12

theon


People also ask

Which Scala version does sbt use?

We recommend that you upgrade to sbt versions 1.0 and later which are compatible with the Scala version 2.12 (requires Java 8).

How does sbt resolve dependencies?

Background. update resolves dependencies according to the settings in a build file, such as libraryDependencies and resolvers . Other tasks use the output of update (an UpdateReport ) to form various classpaths. Tasks that in turn use these classpaths, such as compile or run , thus indirectly depend on update .

What is ThisBuild in sbt?

Typically, if a key has no associated value in a more-specific scope, sbt will try to get a value from a more general scope, such as the ThisBuild scope. This feature allows you to set a value once in a more general scope, allowing multiple more-specific scopes to inherit the value.

What is crossScalaVersions?

SBT's crossScalaVersions allows projects to compile with multiple versions of Scala. However, crossScalaVersions is a hack that reuses projects by mutates settings.


1 Answers

Something like this should work:

libraryDependencies <+= scalaVersion(scalatestDependency(_))

def scalatestDependency(scalaVersion: String) = scalaVersion match {
  case "2.9.2" => "org.scalatest" % "scalatest_2.9.2" % "2.0.M5" % "test"
  case "2.10.0" => "org.scalatest" % "scalatest_2.10.0-RC5" % "2.0.M5-B1" % "test"
}

I assumed that you actually meant that the library versions should be the other way around? :-)

You can see variations on this theme in the ScalaMock 2 build.

like image 65
Paul Butcher Avatar answered Nov 15 '22 09:11

Paul Butcher