Sbt-assembly:如何将所有类组装到单个胖jar中以用于具有依赖性的项目

时间:2017-01-22 08:54:19

标签: scala sbt sbt-assembly

我正在使用sbt-assembly打包我的scala对象,但是我无法弄清楚如何为一个聚合并依赖于许多其他项目的项目组装一个胖jar。

例如,我有:

lazy val a = project in file("project-a")
lazy val poj = (project in file("project-poj")).dependsOn(a)
lazy val root = (project in  file(".")).aggregate(a, poj)

在sbt汇编之后,poj-assembly.jar和root-assembly.jar在项目a中不包含scala类。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

poj-assembly.jar应该包含项目a中的类。如果你不这样做,请在Github上提供一个完整的工作示例,因为那将是sbt-assembly bug。

这是我的演示。

演示

$ sbt
> poj/assembly
[info] Compiling 1 Scala source to /xxx/project-poj/target/scala-2.12/classes...
[info] Including: scala-library-2.12.1.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] SHA-1: 58a67743530fef8d0713759060c685c536d29591
[info] Packaging /xxx/project-poj/target/scala-2.12/poj-assembly-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 2 s, completed Jan 22, 2017 8:05:29 AM
> exit
$ java -jar project-poj/target/scala-2.12/poj-assembly-0.1.0-SNAPSHOT.jar
A(1)

如上所述,它显示A(1),这是我在项目a中定义的类。以下是构建中的文件。

项目/ build.properties

sbt.version=0.13.13

项目/ assembly.sbt

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")

build.sbt

lazy val root = (project in file("."))
  .aggregate(a, poj)
  .settings(
    organization in ThisBuild := "com.example",
    scalaVersion in ThisBuild := "2.12.1",
    version      in ThisBuild := "0.1.0-SNAPSHOT",
    name := "Hello"
  )

lazy val a = (project in file("project-a"))

lazy val poj = (project in file("project-poj"))
  .dependsOn(a)

项目-A / A.scala

package example

case class A(a: Int)

项目POJ / Hello.scala

package example

object Hello extends App {
  println(example.A(1))
}