使用 sbt 进行多项目构建

using sbt for multi project build

我对 SBT 完全陌生。假设我有三个 Scala 项目:project_a、project_b、project_c。我应该如何将所有三个项目构建到一个 jar 文件中?假设我使用 project_a 作为根项目。目录结构如

--project_a
  --build.sbt
--project_b
--project_c

按照 sbt webiste 上的说明,我创建了一个 build.sbt 文件,看起来像

lazy val root = (project.in(file("."))).aggregate(project_b, project_c)
lazy val project_b = project
lazy val project_c = project

我把build.sbt放在了project_a下面。当我在 project_a 下 运行 sbt clean compile 时,会在文件夹 project_a 下创建一个新的(有点空)project_b 和 project_c 文件夹。但是,在 build.sbt 文件中,我的意思是 project_b 和 project_c 指的是我已经创建的包含源代码和测试代码的原始文件夹,它们在 project_a 之外.

谁能告诉我我做错了什么?

谢谢

首先,您的 multi-project 设置不正确。

入门指南说:

Aggregation means that running a task on the aggregate project will also run it on the aggregated projects.

  • 如果您有 project_a 使用 project_bproject_c,那么您还需要 root project_aproject_bproject_c
  • Root 可以聚合所有三个(a、b 和 c),但它只聚合给 sbt shell 的命令,例如同时编译所有三个。
  • project_a 应设置为依赖于 project_bproject_c.

这是一个例子:

lazy val commonSettings = Seq(
  scalaVersion := "2.11.4",
  organization := "com.example"
)

lazy val root = (project in file(".")).
  aggregate(project_a, project_b, project_c).
  settings(commonSettings: _*)

lazy val project_a = project.
  dependsOn(project_b, project_c).
  settings(commonSettings: _*).
  settings(
    // your settings here
  )

lazy val project_b = project.
  settings(commonSettings: _*)

lazy val project_c = project.
  settings(commonSettings: _*)

How should I go about building all three projects into one jar file?

  1. 如果您只想从自己的项目中获取 *.class 文件,您可以在 Macro Projects.
  2. 上查看示例
  3. 如果你想要*.class文件和库依赖,你需要sbt-assembly。