本文介绍了如何拥有具有多个 Scala 版本的 SBT 子项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Scala 2.10 的项目和一个使用 Scala 2.11 的项目.它们依赖于一个可以同时编译的公共项目.

I have a project using Scala 2.10 and one using 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"
)

然后

$ 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

我怎样才能让 baz 被两个项目使用?

How can I have baz usable by both projects?

推荐答案

我创建了一个 SBT 插件 为此.

I created an SBT plugin for this.

project/plugins.sbt

resolvers += Resolver.sonatypeRepo("releases")

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

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")

还需要几行代码,但现在一切都按预期编译:sbt foo/compile 有效,sbt bar/compile 有效.

It takes a couple more lines, but now everything compiles as expected: sbt foo/compile works, and sbt bar/compile works.

您不必记住唯一的命令,也没有来自 crossPath := false 的错误,并且与 ++ 不同的是,这是可并行化的:sbt compile 将使用正确的 Scala 版本同时编译 foobarbaz.

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.

这篇关于如何拥有具有多个 Scala 版本的 SBT 子项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:43