scalasbtcross-build

SBT: Cross build project for two Scala versions with different dependencies


I have the following use case. I would like to build the same Scala project for scala 2.10 and 2.12. When doing so I would like to specify some of the dependencies for the 2.10 version as provided whereas I'd like to have those compiled in the jar for 2.12.

I was looking at SBT's docs and found how I can split a build.sbt into separate declarations but those always get mentioned as sub-modules. In my case I'd like to cross-build the whole app - not specific parts of it.

Any hints or resources will be appreciated.


Solution

  • You can assemble the libraryDependencies depending on the Scala version. Simplified example, may not build..:

    libraryDependencies := {
      CrossVersion.partialVersion(scalaVersion.value) match {
        case Some((2, scalaMajor)) if scalaMajor == 12 =>
          libraryDependencies.value ++ Seq(
            "your.org" %% "your-lib" % "1.0")
        case Some((2, scalaMajor)) if scalaMajor == 10 =>
          libraryDependencies.value ++ Seq(
            "your.org" %% "your-lib" % "1.0" % provided)
        case _ => Seq()
      }
    }
    

    For a full example, see http://github.com/scala/scala-module-dependency-sample