带有子模块和多个 Scala 版本的 sbt

带有子模块和多个 Scala 版本的 sbt

问题描述:

我正在使用这些模块和依赖项构建 Scala 应用程序:

I'm building a scala applications with these module and dependencies :

  • 共享库common"
  • 模块A"依赖于common"并且只能在scala 2.10中构建
  • 模块B"依赖于common"并且只能在scala 2.11+中构建

我试图在一个 sbt 构建中包含所有 3 个模块:

I'm trying to have all the 3 modules in a single sbt build :

import sbt._
import Keys._

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

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

lazy val A = (project in file("A"))
             .dependsOn(common)

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

我已经阅读了有关 crossScalaVersions 的内容,但是无论我在 root 构建中尝试过什么组合,或者在共同点等中尝试过什么组合,我都无法使其正常工作.

I've read things about crossScalaVersions, but whatever combination I try in root build, or in common, etc, I can't manage to make this work properly.

有什么线索吗?顺便说一下,我使用的是 sbt 0.13.8.

Any clue ? I'm using sbt 0.13.8 by the way.

使用裸 sbt 这可能是不可能的.

代表这种情况的示例 build.sbt 文件:

lazy val common = (project in file("common"))
                  .settings(crossScalaVersions := Seq("2.10.6", "2.11.8"))

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

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

这会正常工作.

现在.任何项目的编译都会导致创建一个包.即使对于 root.如果您按照控制台操作,在某些时候它会说:

Now. A compilation of any project leads to a creation of a package. Even for the root. If you follow the console, at some point it says:

Packaging /project/com.github.atais/target/scala-2.10/root_2.10-0.1.jar

如你所见,sbt 需要决定一些 Scala 版本,只是为了构建这个 jar!也就是说,您的项目 AB 必须 有一个通用的 Scala 版本,以便它们可以聚合到一个通用的项目 root代码>.

So as you see sbt needs to decide on some Scala version, just to build this jar! That being said your projects A and B must have a common Scala version so they can be aggregated in a common project root.

因此,你不能:

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

如果他们不共享任何可以构建的 Scala 版本.

if they do not share any Scala version they could be built with.

您可以使用 sbt-cross 插件来帮助您.

You can use sbt-cross plugin to help you out.

project/plugins.sbt中添加

addSbtPlugin("com.lucidchart" % "sbt-cross" % "3.2")

并以下列方式定义您的 build.sbt:

And define your build.sbt in a following way:

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)

然后它就起作用了:-)!

And then it works :-)!