使用 Simple Build Tool 制作独立 jar [英] Making stand-alone jar with Simple Build Tool
问题描述
有没有办法告诉sbt将所有需要的库(scala-library.jar)打包到主包中,这样它就独立了?(静态?)
Is there a way to tell sbt to package all needed libraries (scala-library.jar) into the main package, so it is stand-alone? (static?)
推荐答案
Edit 2011:
从那时起,retronym(发布了早在 2010 年就在本页回答),制作了这个 sbt-plugin sbt-onejar",现在在它的 GitHub 上的新地址,更新了 SBT 0.12 的文档.
Edit 2011:
Since then, retronym (which posted an answer in this page back in 2010), made this sbt-plugin "sbt-onejar", now in its new address on GitHub, with docs updated for SBT 0.12.
使用 One-JAR™
onejar-sbt
是一个简单的构建工具插件,用于构建单个可执行 JAR,其中包含作为嵌套 JAR 的所有代码和依赖项.
当前使用 One-JAR 版本 0.9.7.这是包含在插件中的,不需要单独下载.
onejar-sbt
is a simple-build-tool plugin for building a single executable JAR containing all your code and dependencies as nested JARs.
Currently One-JAR version 0.9.7 is used. This is included with the plugin, and need not be separately downloaded.
原答案:
Original answer:
直接地,如果不扩展 sbt(a 自定义操作 在 打包"sbt 操作的模型之后).
Directly, this is not possible without extending sbt (a custom action after the model of the "package" sbt action).
GitHub 提及为码头部署定制的组装任务.不过,您可以根据自己的需要进行调整.
GitHub mentions an assembly task, custom made for jetty deployment. You could adapt it for your need though.
The code is pretty generic (from this post, and user Rio):
project / build / AssemblyProject.scala
import sbt._
trait AssemblyProject extends BasicScalaProject
{
def assemblyExclude(base: PathFinder) = base / "META-INF" ** "*"
def assemblyOutputPath = outputPath / assemblyJarName
def assemblyJarName = artifactID + "-assembly-" + version + ".jar"
def assemblyTemporaryPath = outputPath / "assembly-libs"
def assemblyClasspath = runClasspath
def assemblyExtraJars = mainDependencies.scalaJars
def assemblyPaths(tempDir: Path, classpath: PathFinder, extraJars: PathFinder, exclude: PathFinder => PathFinder) =
{
val (libs, directories) = classpath.get.toList.partition(ClasspathUtilities.isArchive)
for(jar <- extraJars.get ++ libs) FileUtilities.unzip(jar, tempDir, log).left.foreach(error)
val base = (Path.lazyPathFinder(tempDir :: directories) ##)
(descendents(base, "*") --- exclude(base)).get
}
lazy val assembly = assemblyTask(assemblyTemporaryPath, assemblyClasspath, assemblyExtraJars, assemblyExclude) dependsOn(compile)
def assemblyTask(tempDir: Path, classpath: PathFinder, extraJars: PathFinder, exclude: PathFinder => PathFinder) =
packageTask(Path.lazyPathFinder(assemblyPaths(tempDir, classpath, extraJars, exclude)), assemblyOutputPath, packageOptions)
}
这篇关于使用 Simple Build Tool 制作独立 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!