Deploying an assembly jar file with sbt

The assembly-sbt plugin for sbt 0.7, the (not so) simple build tool for scala, creates a nifty fat jar containing everything to run an application anywhere. You may want an additional task to automatically copy it to a server. Here’s how (Linux only, sorry).

The following code copies the assembly jar to a linux computer using ssh. So you need an ssh account there, and you will also need access via a private key. Read this tutorial on how to accomplish this. The key should be password-less, since it will not be possible to enter a password.

Add this to your Project class in project/build/Project.scala. I assume the assembly-sbt plugin is enabled.

Change the properties to match you settings.

import Process._

lazy val deployAction: Task = task {
  val account = "user@server"
  val src = assemblyOutputPath.absolutePath
  val target = "/dir/on/server/" + assemblyJarName
  val target_abs = account + ":" + target
  val keyfile = System.getProperty("user.home") + "/keyfile""Copying " + src + " to " + target_abs)
  List("scp", "-i", keyfile, src, target_abs) ! log

  // Optional. Create symbolic link to last version
  val symlink = "/path/to/symlink""Symlinking it to  " + symlink)
  val cmd = "ln -sf " + target + " " + symlink
  List("ssh", "-i", keyfile, account, cmd) ! log


lazy val deploy = (deployAction && incrementVersion) dependsOn(assembly) describedAs("Builds assembly and moves it to server")

What the code is to take the assembly created by assembly-sbt and copies it to the server using scp. It then will create a symbolic link to the copied file, which therefore always points to the most recent version. After that it automatically increments the application’s version.

Run it using

sbt deploy

Published: August 03 2011