scalasbtcross-build

Scala: how to build Scala project with cross build alongside different library dependencies


i have an sbt Scala project build on scala 2.13.1 i want to build it with scala-2.12.8 because of some library dependency support only for scala 2.12 after doing some research this question is exactly my use case i followed the answer but i am getting an error

error] scala.MatchError: Some((2,13)) (of class scala.Some)
[error]     at $97a4428383eb69f53d6e$.$anonfun$$sbtdef$1(/home/sara/git/gitinterpret/interpret/build.sbt:56)
[error]     at scala.Function1.$anonfun$compose$1(Function1.scala:44)
[error]     at sbt.internal.util.EvaluateSettings$MixedNode.evaluate0(INode.scala:221)
[error]     at sbt.internal.util.EvaluateSettings$INode.evaluate(INode.scala:164)
[error]     at sbt.internal.util.EvaluateSettings.$anonfun$submitEvaluate$1(INode.scala:87)
[error]     at sbt.internal.util.EvaluateSettings.sbt$internal$util$EvaluateSettings$$run0(INode.scala:98)
[error]     at sbt.internal.util.EvaluateSettings$$anon$3.run(INode.scala:94)
[error]     at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[error]     at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[error]     at java.base/java.lang.Thread.run(Thread.java:834)
[error] scala.MatchError: Some((2,13)) (of class scala.Some)
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? 

here is my build.sbt

lazy val scala212 = "2.12.8"

lazy val scala213 = "2.13.1"

scalaVersion := scala213

lazy val supportedScalaVersions = List(scala213, scala212)

lazy val root = (project in file("."))
  .settings(
    crossScalaVersions := supportedScalaVersions,
  )

libraryDependencies := { CrossVersion.partialVersion(scalaVersion.value) match {
  case Some((2,scalaMajor)) if scalaMajor == 12.8 =>
   libraryDependencies.value ++ Seq(
      "org.scala-lang" % "scala-library" % "2.12.8",
      "org.mongodb.scala" %% "mongo-scala-driver" % "2.8.0"
  "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.3.1" % "test"
  case Some((2,scalaMajor)) if scalaMajor == 13.1 =>
    libraryDependencies.value ++ Seq(
      "org.scala-lang" % "scala-library" % "2.13.1",
      "org.mongodb.scala" %% "mongo-scala-driver" % "2.8.0",
      "com.typesafe.akka" %% "akka-actor" % "2.6.3",
    )
}
}

Solution

  • The versioning schema for, say 2.13.1, is

    MAJOR.MINOR.PATCH
      2    13   1
    

    and CrossVersion.partialVersion returns tuple representing (MAJOR, MINOR). Thus try

    CrossVersion.partialVersion(scalaVersion.value) match {
      case Some((2, 12)) => ???
      case Some((2, 13)) => ???
      case _ =>> ???
    }
    

    Addressing the comment, if you want to get at patch version try with regex

    val VersionSchema = """(\d+)\.(\d+)\.(\d+)""".r
    scalaVersion.value match {
      case VersionSchema("2", "13", "1") => ???
      case VersionSchema("2", "12", "8") => ???
      case _ => ???
    }
    

    however there is no need for that because there should be no breaking changes between patch versions.