使用Build.scala进行交叉编译时,如何为每个Scala版本设置不同的scalacOptions?

时间:2014-12-30 01:46:15

标签: scala sbt cross-compiling

使用Scala 2.10和SBT 0.13.2构建时,我想拥有-language:_,但Scala 2.9无法识别。关于交叉编译(Conditional scalacOptions with SBT)的设置不同scalacOptions有一个问题,但它大约是build.sbt。我正在使用Build.scala,因为我正在进行多项目构建。

我试过这个:

  def scalacOptionsVersion(v: String) = {
    Seq(
      "-unchecked",
      "-deprecation",
      "-Xlint",
      "-Xfatal-warnings",
      "-Ywarn-dead-code",
      "-target:jvm-1.7",
      "-encoding", "UTF-8") ++ (
    if (v.startsWith("2.9")) Seq() else Seq("-language:_"))
  }

  override val settings = super.settings ++ Seq(
    ...,
    scalaVersion := "2.10.4",
    scalacOptions <++= scalaVersion(scalacOptionsVersion),
    crossScalaVersions := Seq("2.9.2", "2.10.4", "2.11.4"),
    ...
  )

但是我收到了错误:

[error] /Users/benwing/devel/lemkit/scala/project/build.scala:29: type mismatch;
[error]  found   : sbt.Def.Initialize[Equals]
[error]  required: sbt.Def.Initialize[sbt.Task[?]]
[error] Note: Equals >: sbt.Task[?], but trait Initialize is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]     scalacOptions <++= scalaVersion(scalacOptionsVersion),
[error]                                    ^
[error] one error found

帮助?

1 个答案:

答案 0 :(得分:11)

在SBT 0.13+中,这将起作用:

def scalacOptionsVersion(scalaVersion: String) = {
  Seq(
    "-unchecked",
    "-deprecation",
    "-Xlint",
    "-Xfatal-warnings",
    "-Ywarn-dead-code",
    "-target:jvm-1.7",
    "-encoding", "UTF-8"
  ) ++ CrossVersion.partialVersion(scalaVersion) match {
         case Some((2, scalaMajor)) if scalaMajor == 9 => Nil
         case _ => Seq("-language:_")
       }
}


val appSettings = Seq(
  scalacOptions := scalacOptionsVersion(scalaVersion.value)

  // other settings...
)