sbt在多项目构建中使用不同的Scala版本

时间:2013-02-07 14:29:18

标签: scala sbt multi-project

我有一个使用两个版本的Scala的多项目。 Build.scala看起来像这样:

import sbt._
import Keys._

object Build extends sbt.Build {
  lazy val root: Project = Project(
    id = "scalacolliderugens",
    base = file("."),
    aggregate = Seq(gen, core)
  )

  lazy val gen  = Project(...)
  lazy val core = Project(...)

  val ugenGenerator = TaskKey[Seq[File]]("ugen-generate", "Generate UGen class files")

  def runUGenGenerator(source: File, cp: Seq[File]): Seq[File] = Nil // TODO
}

我使用Scala 2.9.2进行纯源生成项目:

  lazy val gen = Project(
    id = "scalacolliderugens-gen",
    base = file("gen"),
    settings = Project.defaultSettings ++ Seq(
      scalaVersion := "2.9.2",  // !!!
      libraryDependencies ++= Seq(
        "org.scala-refactoring" % "org.scala-refactoring_2.9.1" % "0.4.1"
      ),
      libraryDependencies <+= scalaVersion { sv =>
        "org.scala-lang" % "scala-compiler" % sv
      }
    )
  )

包含生成源的实际项目,针对Scala 2.10进行编译:

 lazy val core = Project(
    id = "scalacolliderugens-core",
    base = file("core"),
    settings = Project.defaultSettings ++ Seq(
      scalaVersion := "2.10.0",  // !!!
      sourceGenerators in Compile <+= (ugenGenerator in Compile),
      ugenGenerator in Compile <<=
        (scalaSource in Compile, dependencyClasspath in Runtime in gen) map {
          (src, cp) => runUGenGenerator(src, cp.files)
        }
      )
    ).dependsOn(gen)

当我编译它时,我得到了警告:

[warn] Binary version (2.9.2) for dependency org.scala-lang#scala-library;2.9.2
[warn]  in de.sciss#scalacolliderugens_2.10;1.0.1 differs from Scala binary \
  version in project (2.10).
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[warn] Binary version (2.9.2) for dependency org.scala-lang#scala-library;2.9.2
[warn]  in de.sciss#scalacolliderugens-gen_2.10;1.0.1 differs from Scala binary \
  version in project (2.10).
[warn] Binary version (2.9.2) for dependency org.scala-lang#scala-compiler;2.9.2
[warn]  in de.sciss#scalacolliderugens-gen_2.10;1.0.1 differs from Scala binary \
  version in project (2.10).
我应该担心吗?我在这里做坏事吗?

3 个答案:

答案 0 :(得分:2)

将以下设置添加到gen项目。

scalaBinaryVersion:= CrossVersion.binaryScalaVersion("2.9.2")

答案 1 :(得分:1)

行...

我有多项目SBT项目,他们通常有SBT设置,部分由所有子项目共享,部分特定于每个子项目。在我的例子中,常见设置包括要使用的Scala版本。

您可以以相同的方式构建规范,并将Scala版本设置放在子项目特定的设置中。如果有必要,您可以进一步对设置进行分层,如果有一些共同的设置,子项目的子集和其他特定于每个子项目的子设置。

答案 2 :(得分:0)

您不能混合来自Scala的不同次要版本的字节码。即,任何2.9.x系列编译器编译的代码与2.10.x(或2.8.x)编译器发出的代码兼容。