Skip to content

Instantly share code, notes, and snippets.

@anvie
Created June 30, 2012 09:16
Show Gist options
  • Save anvie/3023066 to your computer and use it in GitHub Desktop.
Save anvie/3023066 to your computer and use it in GitHub Desktop.
Gather all dependencies jar and generate start script for running, all placed in `target/` dir
lazy val copyDependencies = TaskKey[Unit]("gather-lib")
def gatherDeps = copyDependencies <<= (update, crossTarget, scalaVersion, internalDependencyClasspath in Compile, mainClass in Runtime) map {
(updateReport, out, scalaVer, deps, main_class) =>
var projDeps = Array[String]()
deps.foreach {
depsDir =>
val x = depsDir.data.getCanonicalPath.split("/")
if (x.length > 3){
val projName = x(x.length - 3)
println("Copying `" + out / "lib" / projName + "`...")
IO.copyDirectory(depsDir.data, out / "lib" / projName)
projDeps :+= "lib/" + projName
}
}
updateReport.allFiles foreach {
srcPath =>
val destPath = out / "lib" / srcPath.getName
println("Copying `" + destPath + "`...")
IO.copyFile(srcPath, destPath, preserveLastModified = true)
}
// generate executable script start.sh
println("Generating start.sh...")
val shScript =
"""#!/usr/bin/env sh
|java "$@" -cp "classes:lib/*:%s" %s
""".stripMargin.format(projDeps.reduce(_ + ":" + _), main_class.getOrElse("xxx")).trim + "\n"
IO.write(out / "start.sh", shScript.getBytes)
}
@anvie
Copy link
Author

anvie commented Jun 30, 2012

Add to your project settings:

.settings(gatherDeps)

From sbt console type:

gather-lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment