为子项目交叉编译具有不同crossScalaVersions的聚合项目

时间:2014-12-01 15:07:47

标签: scala sbt

我们有一个项目有几个子项目,可以在Scala 2.10和2.11下编译,一个子项目只编译在2.10(实际上是Scala-Virtualized 2.10.2)和一个子项目只编译在2.11下。有没有一种简单的方法来创建一个聚合项目,它为2.10和2.11交叉构建所有可能的子项目?或者,或者,为2.10和2.11设置不同的默认项目?

特别是here is current Build.scala。如果我将lmsBackend添加到root,我会

> show scalaVersion
[info] common/*:scalaVersion
[info]  2.10.4
[info] lms-backend/*:scalaVersion
[info]  2.10.2
[info] meta/*:scalaVersion
[info]  2.10.4
[info] community-edition/*:scalaVersion
[info]  2.10.4
[info] core/*:scalaVersion
[info]  2.10.4
[info] scalan/*:scalaVersion
[info]  2.10.4
> show crossScalaVersions
[info] common/*:crossScalaVersions
[info]  List(2.10.4, 2.11.5)
[info] lms-backend/*:crossScalaVersions
[info]  List(2.10.2)
[info] meta/*:crossScalaVersions
[info]  List(2.10.4, 2.11.5)
[info] community-edition/*:crossScalaVersions
[info]  List(2.10.4, 2.11.5)
[info] core/*:crossScalaVersions
[info]  List(2.10.4, 2.11.5)
[info] scalan/*:crossScalaVersions
[info]  List(2.10.4, 2.11.5)

SBT能够在此聚合项目上运行updatecompile等。然而,一旦我尝试任何交叉建设,事情就会破裂:

> +update
[info] Setting version to 2.10.4
[info] Reapplying settings...
[info] Set current project to scalan (in build file:/home/aromanov/IdeaProjects/scalan-lite/)
...
[info] Updating {file:/home/aromanov/IdeaProjects/scalan-lite/}lms-backend...
[info] Resolving org.scala-lang.virtualized#scala-library;2.10.4 ...
[warn]  module not found: org.scala-lang.virtualized#scala-library;2.10.4
...
[info] Resolving org.scala-lang.virtualized#scala-compiler;2.10.4 ...
[warn]  module not found: org.scala-lang.virtualized#scala-compiler;2.10.4
...
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: org.scala-lang.virtualized#scala-library;2.10.4: not found
[warn]  :: org.scala-lang.virtualized#scala-compiler;2.10.4: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn] 
[warn]  Note: Unresolved dependencies path:
[warn]      org.scala-lang.virtualized:scala-library:2.10.4 ((sbt.Classpaths) Defaults.scala#L1169)
[warn]        +- com.huawei.scalan:lms-backend_2.10:0.2.6-SNAPSHOT
[warn]      org.scala-lang.virtualized:scala-compiler:2.10.4
[warn]        +- com.huawei.scalan:lms-backend_2.10:0.2.6-SNAPSHOT
[trace] Stack trace suppressed: run last lms-backend/*:update for the full output.
[error] (lms-backend/*:update) sbt.ResolveException: unresolved dependency: org.scala-lang.virtualized#scala-library;2.10.4: not found
[error] unresolved dependency: org.scala-lang.virtualized#scala-compiler;2.10.4: not found
[error] Total time: 2 s, completed Jan 28, 2015 1:33:49 PM

show scalaVersion现在显示所有子项目的2.10.4。有没有办法在聚合项目中包含lms-backend并仍然可以避免这个问题?

2 个答案:

答案 0 :(得分:5)

我有similar question。我的问题还有类路径依赖(dependsOn)的额外复杂性,而不仅仅是聚合,但我的问题的一个解决方案很好地修复了这个问题。

sbt-doge,一个看似可爱的GitHub项目,用一个可互换的前缀取代+的实现:很多,所以,非常。

  

+ cross building operator的当前实现不考虑子项目的crossScalaVersions。在这个问题得到解决之前,这里有一个替代实现。

该项目的创建者是SBT的主要贡献者之一。


添加

addSbtPlugin("com.eed3si9n" % "sbt-doge" % "0.1.3")

project/plugins.sbt

然后

> very compile

答案 1 :(得分:1)

我认为sbt-doge可能不足以满足您的要求,因为此插件也无法满足我的要求。

我建议sbt-cross

此插件允许您在模块之间提供更大的灵活性,并且它提供了一种方法来使用不同Scala版本的aggregate模块,而无需任何共同版本!

示例:

lazy val common = (project in file("common")).cross

lazy val common_2_11 = common("2.11.8")
lazy val common_2_10 = common("2.10.6")

lazy val A = (project in file("A"))
             .settings(scalaVersion := "2.10.6")
             .dependsOn(common_2_10)

lazy val B = (project in file("B"))
             .settings(scalaVersion := "2.11.8")
             .dependsOn(common_2_11)

lazy val root = (project in file("."))
                .aggregate(common, A, B)

我已经发布了类似的answer

享受!