简体   繁体   English

如何拥有多个Scala版本的SBT子项目?

[英]How to have SBT subproject with multiple Scala versions?

I have a project using Scala 2.10 and one using Scala 2.11. 我有一个使用Scala 2.10的项目和一个使用Scala 2.11的项目。 They depend on a common project, which can compile with both. 它们依赖于一个可以用两者编译的通用项目。

lazy val foo = (project in file("foo")).dependsOn(baz).settings(
  scalaVersion := "2.10.4"
)

lazy val bar = (project in file("bar")).dependsOn(baz).settings(
  scalaVersion := "2.11.4"
)

lazy val baz = (project in file("baz")).settings(
  crossScalaVersions := Seq("2.10.4", "2.11.4"),
  scalaVersion := "2.10.4"
)

And then 接着

$ sbt bar/update
[info] Updating {file:/home/paul/Private/test/}bar...
[info] Resolving baz#baz_2.11;0.1-SNAPSHOT ...
[warn]  module not found: baz#baz_2.11;0.1-SNAPSHOT
[warn] ==== local: tried
[warn]   /home/paul/.ivy2/local/baz/baz_2.11/0.1-SNAPSHOT/ivys/ivy.xml
[warn] ==== public: tried
[warn]   http://repo1.maven.org/maven2/baz/baz_2.11/0.1-SNAPSHOT/baz_2.11-0.1-SNAPSHOT.pom
[info] Resolving jline#jline;2.12 ...
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: baz#baz_2.11;0.1-SNAPSHOT: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[trace] Stack trace suppressed: run last bar/*:update for the full output.
[error] (bar/*:update) sbt.ResolveException: unresolved dependency: baz#baz_2.11;0.1-SNAPSHOT: not found
[error] Total time: 1 s, completed Jan 13, 2015 11:42:51 AM

How can I have baz usable by both projects? 我怎么能有baz通过这两个项目使用吗?

Yevgeniy Mordovkin's proposed solution, to declare crossPaths := false in the baz project, works. Yevgeniy Mordovkin建议的解决方案,在baz项目中声明crossPaths := false ,有效。

Another thing you might do, is to prepend a + before the command: sbt '+ bar/update' . 你可能做的另一件事是在命令之前加上一个+sbt '+ bar/update' That will build baz for all declared Scala versions. 这将为所有声明的Scala版本构建baz

I created an SBT plugin for this. 我为此创建了一个SBT插件

project/plugins.sbt 项目/ plugins.sbt

resolvers += Resolver.sonatypeRepo("releases")

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

build.sbt build.sbt

lazy val foo = (project in file("foo")).dependsOn(baz_2_10).settings(
  scalaVersion := "2.10.4"
)

lazy val bar = (project in file("bar")).dependsOn(baz_2_11).settings(
  scalaVersion := "2.11.5"
)

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

lazy val baz_2_10 = baz("2.10.4")

lazy val baz_2_11 = baz("2.11.5")

It takes a couple more lines, but now everything compiles as expected: sbt foo/compile works, and sbt bar/compile works. 它需要更多行,但现在一切都按预期编译: sbt foo/compile工作, sbt bar/compile工作。

You don't have to remember unique commands, you don't have bugs from crossPath := false , and unlike ++ , this is parallelizable: sbt compile will compile foo , bar , and baz with the correct Scala versions concurrently. 您不必记住唯一命令,您没有来自crossPath := false ,并且与++不同,这是可并行化的: sbt compile将同时编译具有正确Scala版本的foobarbaz

I had a similar setup, and got it work like so: 我有类似的设置,并得到它的工作:

lazy val baz = (project in file("baz")).settings(
  crossScalaVersions := Seq("2.10.4", "2.11.4")
)
lazy val bar = (project in file("bar")).dependsOn(baz).settings(
  scalaVersion := "2.10.4"
)
lazy val foo = (project in file("foo")).dependsOn(baz).settings(
  scalaVersion := "2.11.4"
)

And building with 和建设

sbt '++ 2.10.4 baz/compile' 'bar/compile' '++ 2.11.4 baz/compile' 'foo/compile'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM