Is it possible to enforce different scala version for a specific dependency using SBT

1.5k views Asked by At

I'm pretty new to scala. While upgrading a multi-module project to Scala 2.13, I found this dependency that is compiled in Scala 2.12 which throws class not found exception during runtime

java.lang.NoClassDefFoundError: scala/collection/mutable/ArrayOps$ofRef

This class is removed in 2.13. It is available only until 2.12. I am looking for a way to enforce v2.12 to compile only this dependency.

I tried to use cross-building but that does not work for a core library, because the dependency url constructed using:

"org.scala-lang" % "scala-library" % scalaVersion.value

looks like

https://repo1.maven.org/maven2/org/scala-lang/scala-library_2.12.15/2.12.15/scala-library_2.12.15-2.12.15.pom

Also, cross-building seems to be the way to allow compiling sub-modules with different scala versions with their compatible dependency versions, not meant for enforcing scala versions on individual dependencies.

Edit 1: This is the build definition:

   root
    |
    main
    |---dependency w/o 2.13 build
    |
    acceptanceTests
    |---dependency w/o 2.13 build
    |
    (other modules)

The dependency is an internal commons library. This uses the class scala/collection/mutable/ArrayOps during compile time. From scala-lang -> scala-library.

My questions:

  1. Is it even possible to do this? Or is my only option to downgrade to 2.12 as mentioned here
  2. Why 'core' libraries do not follow the url patters of external libraries like: [organisation]/[module](_[scalaVersion])(_[sbtVersion])/[revision]. `Instead it looks like https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.15/scala-library-2.12.15.pom
1

There are 1 answers

1
Steve Waldman On

You can do stuff like this:

libraryDependencies ++= {
  CrossVersion.partialVersion(Keys.scalaVersion.value) match {
    case Some((2, 12)) => Seq(
      "com.typesafe.play" %% "play-json"  % "2.6.13"
    )
    case Some((2, 11)) => Seq( 
      "com.typesafe.play" %% "play-json" % "2.5.18"
    )
    case _ => Seq (
      "com.typesafe.play" %% "play-json" % "2.4.11"
    )
  }
}