捆绑依赖jar到输出jar

时间:2014-05-08 21:59:46

标签: scala sbt

我正在使用一个将插件从目录加载为jar的应用程序。插件应具有以下布局:

plugin.myplugin.jar/
  com.xxx.xxx/<compiled classes for plugin>
  META-INF/MANIFEST.MF (defines the main entry point for the plugin)
  lib/
     com.fasterxml.jackson.core.jackson-core-2.3.1.jar
     ...
     ...
     com.netflix.rxjava.rxjava-core-0.16.1.jar
     ...
     <all dependencies used by the plugin in their original jar format>

我可以通过使用sbt-native-packager插件手动生成顶级plugin.myplugin.jar,然后将jar文件从target / universal / stage / lib复制到plugin.myplugin中的lib目录中。罐子使用7-zip之类的东西。手动执行此操作......但我尝试自动执行此任务,目前我对如何覆盖sbt-native-packager任务或编写自己的任务有点不知所措。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这就是你用纯SBT 0.13.x实现你想要的东西(这个例子一般适用于旧版本,但也许你必须使用不同的运算符)。

build.sbt

import Path.flat

libraryDependencies ++= Seq(
  "com.fasterxml.jackson.core" % "jackson-core" % "2.3.1",
  "com.netflix.rxjava" % "rxjava-core" % "0.16.1"
  // maybe more dpendencies
)

packageOptions in (Compile, packageBin) +=
        Package.ManifestAttributes("PluginMainClass" -> "com.xxx.xxx.Class")

// this will copy all managed jars to the ./lib in your jar
mappings in (Compile, packageBin) ++= {
  val cp = (managedClasspath in Compile).value.files
  cp pair flatRebase("/lib")
}

// this will move all your compiled classes to folder ./com.xxx.xxx in your jar
mappings in (Compile, packageBin) ++= {
  val compiledClasses = (products in Compile).value ** "*.class"
  val classDirectoryBase = (classDirectory in Compile).value
  compiledClasses pair rebase(classDirectoryBase, "com.xxx.xxx")
}

然后,您可以使用package来构建jar。在上面的示例中,jar将如下所示:

  Length      Date    Time    Name
---------  ---------- -----   ----
      301  2014-05-09 20:13   META-INF/MANIFEST.MF
        0  2014-05-09 20:13   /lib/
  7126003  2013-09-27 11:44   /lib/scala-library.jar
   197986  2013-12-28 02:01   /lib/jackson-core-2.3.1.jar
   663553  2014-01-15 08:17   /lib/rxjava-core-0.16.1.jar
---------                     -------
  7987843                     5 files

清单看起来像这样

Manifest-Version: 1.0
Implementation-Vendor: default
Implementation-Title: q-23553321
Implementation-Version: 0.1-SNAPSHOT
Implementation-Vendor-Id: default
PluginMainClass: com.xxx.xxx.Class // MAIN CLASS OF THE PLUGIN
Specification-Vendor: default
Specification-Title: q-23553321
Specification-Version: 0.1-SNAPSHOT

修改

  

是否可以在多模块构建中获取已编译的类   虽然? IE如果我有/ modules / X,Y,Z和我想要编译的类   来自&#34; com.xxx.xxx&#34;的捆绑项目在神器?

一般的答案是肯定的,如果这些类是唯一的。为此,您可以使用Scopes。在build.sbt中,使用以下内容替换类的映射:

// define the configuration axis
val anyProjectsCompile = ScopeFilter(inAnyProject, inConfigurations(Compile))

// we want to have class directory and products together to use rebase in the next step
val classDirectoryProducts = Def.task {
  (classDirectory.value, products.value)
}

mappings in (Compile, packageBin) ++= {
  classDirectoryProducts.all(anyProjectsCompile).value.flatMap { case (classDir, prods) =>
    val compiledClasses = (prods ** "*.class").get
    compiledClasses pair rebase(classDir, "com.xxx.xxx")
  }
}