Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making stand-alone jar with Simple Build Tool

Tags:

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?)

like image 501
Łukasz Lew Avatar asked May 22 '10 09:05

Łukasz Lew


1 Answers

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.

Packages your project using One-JAR™

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:

Directly, this is not possible without extending sbt (a custom action after the model of the "package" sbt action).

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)  } 
like image 69
VonC Avatar answered Sep 29 '22 03:09

VonC