使用与当前不同的 scalaVersion 评估任务

Evaluating a task using a different scalaVersion than the current

在多项目构建中,我有两个项目 A 和 B,它们交叉编译为 Scala 2.12 和 Scala 2.13。我想向项目 A 添加一个任务,该任务依赖于 B 的 Scala 2.12 类路径(B / Compile / fullClasspath),而不管 A 中使用的 Scala 版本:

ThisBuild / scalaVersion := "2.13.2"
ThisBuild / crossScalaVersions := Seq("2.13.2", "2.12.10")

val foo = taskKey[Unit]("foo-task")

lazy val B = project.in(file("B"))

lazy val A = project.in(file("A")).settings(
  foo := {
    println((B / Compile / fullClasspath).value)
  }
)

原样,运行 A/foo 将打印 B 的 scala-2.13 类路径。如何更改它以便打印 B 的 Scala 2.12 类路径?

上下文:在 A 的源代码生成期间,我想在 SBT 中从 B 执行代码,所以我需要 B 的 2.12 类,而不管正在编译的 A 的版本如何。

您可以使用 sbt-cross 而不是 crossScalaVersions,这样您就可以为每个 scala 版本创建一个单独的子项目。

https://github.com/lucidsoftware/sbt-cross