简体   繁体   English

是否可以使用 SBT 为特定依赖项强制执行不同的 scala 版本

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

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我是 scala 的新手。在将多模块项目升级到 Scala 2.13 时,我发现在 Scala 2.12 中编译的依赖项在运行时抛出class not found异常

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

This class is removed in 2.13.这个 class 在 2.13 中被移除。 It is available only until 2.12 .它仅在2.12之前可用。 I am looking for a way to enforce v2.12 to compile only this dependency.我正在寻找一种方法来强制 v2.12编译此依赖项。

I tried to use cross-building but that does not work for a core library, because the dependency url constructed using:我尝试使用交叉构建,但这对核心库不起作用,因为依赖项 url 使用以下方法构建:

"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.此外,交叉构建似乎是允许编译具有不同 scala 版本及其兼容依赖版本的子模块的方式,而不是为了在各个依赖项上强制执行 scala 版本。

Edit 1: This is the build definition:编辑 1:这是构建定义:

   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.这在编译期间使用 class scala/collection/mutable/ArrayOps From scala-lang -> scala-library .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.12 的唯一选择,如此所述
  2. Why 'core' libraries do not follow the url patters of external libraries like: [organisation]/[module](_[scalaVersion])(_[sbtVersion])/[revision] .为什么“核心”库不遵循外部库的 url 模式,例如: [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 `相反,它看起来像https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.15/scala-library-2.12.15.pom

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"
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM