SBT Scala 跨版本,具有聚合和依赖关系 [英] SBT Scala cross versions, with aggregation and dependencies
问题描述
我正在为 crossScalaVersions
如何处理子项目而苦苦挣扎.
I am struggling with how crossScalaVersions
works with subprojects.
我有一个用 2.10 (foo) 编译的项目和一个用 2.11 (bar) 编译的项目.他们共享一个交叉编译的项目(通用).
I have a project that compiles with 2.10 (foo) and a project that compiles with 2.11 (bar). They share a cross compiled project (common).
如何编译项目 foo 和 bar?
How can I compile projects foo and bar?
build.sbt
lazy val root = (project in file(".")).aggregate(foo, bar).settings(
crossScalaVersions := Seq("2.10.4", "2.11.4")
)
lazy val foo = (project in file("foo")).dependsOn(common).settings(
crossScalaVersions := Seq("2.10.4"),
scalaVersion := "2.10.4"
)
lazy val bar = (project in file("bar")).dependsOn(common).settings(
crossScalaVersions := Seq("2.11.4"),
scalaVersion := "2.11.4"
)
lazy val common = (project in file("common")).settings(
crossScalaVersions := Seq("2.10.4", "2.11.4")
)
project/build.properties
sbt.version=0.13.7
foo/src/main/scala/Foo.scala
object Foo {
<xml>{new C}</xml>
}
bar/src/main/scala/Bar.scala
case class Bar(a: C, b: C, c: C, d: C, e: C, f: C, g: C,
h: C, i: C, j: C, k: C, l: C, m: C, n: C, o: C, p: C,
q: C, r: C, s: C, t: C, u: C, v: C, w: C, x: C, y: C,
z: C)
common/src/main/scala/Common.scala
class C {}
<小时>
尝试 1
$ sbt compile
[info] Resolving jline#jline;2.12 ...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: common#common_2.11;0.1-SNAPSHOT: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn]
[warn] Note: Unresolved dependencies path:
[warn] common:common_2.11:0.1-SNAPSHOT
[warn] +- bar:bar_2.11:0.1-SNAPSHOT
sbt.ResolveException: unresolved dependency: common#common_2.11;0.1-SNAPSHOT: not found
尝试 2
$ sbt +compile
[error] /home/paul/test/bar/src/main/scala/Bar.scala:1: Implementation restriction: case classes cannot have more than 22 parameters.
[error] case class Bar(a: C, b: C, c: C, d: C, e: C, f: C, g: C,
[error] ^
[error] one error found
[error] (bar/compile:compile) Compilation failed
尝试 3
$ sbt foo/compile bar/compile
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: common#common_2.11;0.1-SNAPSHOT: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn]
[warn] Note: Unresolved dependencies path:
[warn] common:common_2.11:0.1-SNAPSHOT
[warn] +- bar:bar_2.11:0.1-SNAPSHOT
sbt.ResolveException: unresolved dependency: common#common_2.11;0.1-SNAPSHOT: not found
尝试 4
$ sbt +foo/compile +bar/compile
[error] /home/paul/test3/foo/src/main/scala/Foo.scala:2: To compile XML syntax, the scala.xml package must be on the classpath.
[error] Please see http://docs.scala-lang.org/overviews/core/scala-2.11.html#scala-xml.
[error] <xml>{new C}</xml>
[error] ^
[error] one error found
[error] (foo/compile:compile) Compilation failed
尝试 5
我什至尝试使用相同的基目录但不同的 Scala 版本定义 common_2_10
和 common_2_11
项目.我记得读到目标是由 Scala 版本命名的,但 SBT 说存在冲突.
I even tried defining common_2_10
and common_2_11
projects with that same base directory but different scala versions. I recall reading that targets are namespaced by Scala version, but SBT says there is a conflict.
$ sbt
[error] Overlapping output directories:/home/paul/test3/common/target:
[error] ProjectRef(file:/home/paul/test3/,common_2_10)
[error] ProjectRef(file:/home/paul/test3/,common_2_11)
<小时>
我唯一要做的就是手动指定版本:
The only thing I've gotten to work is manually specifying versions:
$ sbt ++2.10.4 foo/compile ++2.11.4 bar/compile
但这是很多命令,永远不能使用并行性,并且避免了(1)项目聚合和(2)交叉构建的整个使用.
But this is a lot of commands, can never use parallelism, and obviates the whole use of (1) project aggregation and (2) cross building.
我是否遗漏了一些关于 crossScalaVersions
意图的基本内容?或者有没有办法让它与 SBT 的其余部分很好地配合,并且让我编译我的异构项目?
Am I missing something fundamental about the intent of crossScalaVersions
? Or is there a way to have it play well with the rest of SBT, and for me to compile my heterogeneous projects?
推荐答案
我最终声明了两次通用,每个版本一次.
I wound up declaring common twice, once for each version.
lazy val root = (project in file(".")).aggregate(foo, bar)
lazy val foo = (project in file("foo")).dependsOn(common_2_10).settings(
scalaVersion := "2.10.4"
)
lazy val bar = (project in file("bar")).dependsOn(common_2_11).settings(
scalaVersion := "2.11.4"
)
def commonProject = (project in file("common")).settings(
target := baseDirectory.value / s"target-${scalaVersion.value}"
)
lazy val common_2_10 = commonProject.settings(
scalaVersion := "2.10.4"
)
lazy val common_2_11 = commonProject.settings(
scalaVersion := "2.11.4"
)
请注意,我必须使目标目录不同,否则 SBT 会因为它们重叠而拒绝它.
Note that I had to make the target directories different, or else SBT would reject it because they overlapped.
另请注意,def
使 commonProject
不包括 SBT 对项目定义的神奇(基于反射)搜索.
Also note that def
makes commonProject
not included the SBT's magical (reflection-based) search for project definitions.
这不是最漂亮的,但它健壮、可读且合理.所有命令/任务都按预期工作.
This isn't the prettiest, but it is robust, readable, and reasonable. All commands/tasks work as one might expect.
在某种程度上,这比 crossScalaVersions
更好,因为现在可以并行编译 2.10 和 2.11 项目,这不会发生在 crossScalaVersions
:)
In one way this is even better than crossScalaVersions
, in that 2.10 and 2.11 projects can now be compiled in parallel, which does not happen with crossScalaVersions
:)
我创建了一个 SBT 插件 sbt-cross 来帮助解决这个问题.
I created an SBT plugin, sbt-cross, to help out with this.
这篇关于SBT Scala 跨版本,具有聚合和依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!