我有一个 Scala 项目,它分为几个子项目:
lazy val core: Seq[ProjectReference] = Seq(common, json_scalaz7, json_scalaz)
我想做
core
在我目前使用的 Scala 版本上惰性 val 有条件,所以我试过这个:lazy val core2: Seq[ProjectReference] = scalaVersion {
case "2.11.0" => Seq(common, json_scalaz7)
case _ => Seq(common, json_scalaz7, json_scalaz)
}
简单来说,我想排除
json_scalaz
对于 Scala 2.11.0(当 scalaVersion
设置的值为 "2.11.0"
时)。然而,这给了我以下编译错误:
[error] /home/diego/work/lift/framework/project/Build.scala:39: type mismatch;
[error] found : sbt.Project.Initialize[Seq[sbt.Project]]
[error] required: Seq[sbt.ProjectReference]
[error] lazy val core2: Seq[ProjectReference] = scalaVersion {
[error] ^
[error] one error found
知道如何解决这个问题吗?
更新
我正在使用 sbt 版本 0.12.4
这个项目是 Lift 项目,它针对
"2.10.0", "2.9.2", "2.9.1-1", "2.9.1" and now we are working on getting it to compile with 2.11.0
编译.所以创建一个 compile all 任务是不切实际的,因为它会花费很长时间。更新 2
我希望有这样的事情:
lazy val scala_xml = "org.scala-lang.modules" %% "scala-xml" % "1.0.1"
lazy val scala_parser = "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.1"
...
lazy val common =
coreProject("common")
.settings(description := "Common Libraties and Utilities",
libraryDependencies ++= Seq(slf4j_api, logback, slf4j_log4j12),
libraryDependencies <++= scalaVersion {
case "2.11.0" => Seq(scala_xml, scala_parser)
case _ => Seq()
}
)
但对于项目列表
请注意如何根据 scala 版本添加 scala_xml 和 scala_parser_combinator 库
可以看到完整的构建文件here
最佳答案
交叉构建项目
Simply speaking, I'd like to exclude json_scalaz for Scala 2.11.0
sbt 中对此的内置支持称为交叉构建,在 Cross-Building a Project 中有描述。 .以下是稍加修正的部分:
Define the versions of Scala to build against in the
crossScalaVersions
setting. For example, in a .sbt build definition:
crossScalaVersions := Seq("2.10.4", "2.11.0")
To build against all versions listed
crossScalaVersions
, prefix the action to run with+
. For example:
> +compile
多项目构建
sbt 还内置支持跨多个项目聚合任务,描述为 Aggregation .如果您最终需要的是正常的内置任务,例如
compile
和 test
,您可以在没有 json_scalaz
的情况下设置虚拟聚合.lazy val withoutJsonScalaz = (project in file("without-json-scalaz")).
.aggregate(liftProjects filterNot {_ == json_scalaz}: _*)
在 shell 中,您应该可以将其用作:
> ++2.11.0
> project withoutJsonScalaz
> test
从多个范围获取值
您可能感兴趣的另一个功能是 ScopeFilter .这有能力在通常的聚合和交叉构建之外遍历多个项目。您需要创建一个类型为
ScopeFilter
的设置。并根据 scalaBinaryVersion.value
进行设置.使用范围过滤器,您可以:val coreProjects = settingKey[ScopeFilter]("my core projects")
val compileAll = taskKey[Seq[sbt.inc.Analysis]]("compile all")
coreProjects := {
(scalaBinaryVersion.value) match {
case "2.10" => ScopeFilter(inProjects(common, json_scalaz7, json_scalaz))
}
}
compileAll := compileAllTask.value
lazy val compileAllTask = Def.taskDyn {
val f = coreProjects.value
(compile in Compile) all f
}
在这种情况下
compileAll
与 +compile
的效果相同,但你可以聚合结果并做一些有趣的事情,比如 sbt-unidoc .
关于sbt - 根据设置的值在构建中包含项目,例如Scala版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23452098/