Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

scala - Making stand-alone jar with Simple Build Tool

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...