A better solution for building multiple Scala versions (cross compiling) in SBT.
crossScalaVersions allows projects to compile with multiple versions of Scala.
crossScalaVersions is a hack that reuses projects by mutates settings. This causes problems:
- Performance - Each Scala version is handled sequentially (not in parallel).
- Subprojects - Project classpath dependencies can be tricky.
- Aggregation - Aggregation doesn't take into account
crossScalaVersionsof subprojects, hence sbt-doge.
- Cross paths - Very many tasks don't play nicely. E.g. if you use sbt-native-packager and
+debian:packageBin, you'll build two debs on top of each other.
- General funniness - For example, sbt-sonatype requires extra wrangling to work with
sbt-cross solves all by simply splitting projects: one for each version of Scala. This works with SBT's core project/task system, rather than fighting it.
Requires SBT 1.3+.
In project/plugins.sbt, add
addSbtPlugin("com.lucidchart" % "sbt-cross" % "4.0")
For the latest development version,
resolvers += Resolver.sonatypeRepo("snapshots") addSbtPlugin("com.lucidchart" % "sbt-cross" % "master-SNAPSHOT")
Suppose there is a project
pipers that uses Scala 2.11, a project
drummers that uses Scala 2.12, and they depend on a project
instruments, which compiles with both Scala versions.
This cannot be done with
crossScalaVersions, but it can with sbt-cross.
lazy val pipers = project.dependsOn(instruments_2_11).settings(scalaVersion := "2.11.8") lazy val drummers = project.dependsOn(instruments_2_12).settings(scalaVersion := "2.12.1") lazy val instruments = project.cross lazy val instruments_2_11 = instruments("2.11.8") lazy val instruments_2_12 = instruments("2.12.1")
This defines four projects:
SBT will concurrently compile the right ones in the right order.
sbt pipers/compilewill compile
sbt drummers/compilewill compile
sbt compilewill compile
pipersand, in parallel,
See examples for more.
sbt-cross generalizes this approach to other any cross-compilation-like use. You can even chain cross versions. There is currently no documentation for this, but
LibraryVersionAxis is an example.
We welcome issues, questions, and contributions on our GitHub project (https://github.com/lucidsoftware/sbt-cross).