使用sbt-assembly创建uberjar只使用多项目构建中的jar

时间:2016-05-11 22:12:30

标签: scala sbt sbt-assembly

我有一个多项目构建,我正在使用sbt-assembly来创建胖罐。我想创建两个jar:一个ja​​r只有项目创建的jar,然后是另一个jar只有依赖项。我似乎能够很好地创建依赖jar(assemblyPackageDependency),但是我无法弄清楚如何用创建一个jar,只需项目生成的jar。有一个简单的方法吗?

1 个答案:

答案 0 :(得分:0)

  • sbt package packages only the current sub project.
  • sbt assemblyPackageDependency includes only the dependencies. Projects marked as dependsOn are not included.

assembly, assemblyPackageDependency or assemblyPackageScala are tasks which play with settings defined in AssemblyOption to create various packaging configurations. A few relevant options are shown below:

AssemblyOption(assemblyDirectory: File,
  // include compiled class files from itself or subprojects
  includeBin: Boolean = true,
  includeScala: Boolean = true,
  // include class files from external dependencies
  includeDependency: Boolean = true,

A variation of assembly call using config shown below will result in a binary with all sub-projects

assemblyOption in assemblyPackage := {
    val ao = (assemblyOption in assembly).value
    ao.copy(includeBin = true, includeScala = false, includeDependency = false)
  },

However if you want to preserve assembly, a new task assemblyPackage can be created. Snippet from main build.sbt

lazy val assemblyPackage = TaskKey[File]("assemblyPackage", "packages multi-project using sbt-assembly")

lazy val assemblyPackageSettings = Seq(
  // Required packaging configuartion
  assemblyOption in assemblyPackageDependency := {
    val ao = (assemblyOption in assemblyPackageDependency).value
    ao.copy(includeScala = false)
  },
  // Some boiler-plate settings to be copied to new task
  assembledMappings in assemblyPackage       := Assembly.assembledMappingsTask(assemblyPackage).value,
  assemblyPackage                            := Assembly.assemblyTask(assemblyPackage).value,
  test in assemblyPackage := (test in assembly).value,
  assembleArtifact in assemblyPackage := true,
  packageOptions in assemblyPackage      := (packageOptions in (Compile, packageBin)).value,
  assemblyOutputPath in assemblyPackage      := { (target in assembly).value / (assemblyJarName in assemblyPackage).value },
  assemblyJarName in assemblyPackage       := ((assemblyJarName in assemblyPackage)      or (assemblyDefaultJarName in assemblyPackage)).value,
  // Choice of jar name
  assemblyDefaultJarName in assemblyPackage      := { name.value + "-assemblyPackage-" + version.value + ".jar" },
  assemblyOption in assemblyPackage := {
    val ao = (assemblyOption in assembly).value
    ao.copy(includeScala = false, includeDependency = false)
  },
  assemblyPackage := Assembly.assemblyTask(assemblyPackage).value
)

// And use itas
lazy val misc = project.settings(commonSettings ++ assemblyPackageSettings)
  .dependsOn(common)

sbt assemblyPackage will build *-assemblyPackage-*jar files with all dependent sub projects.