Deploying Scala and Play Applications with the Heroku sbt Plugin
Last updated 24 August 2016
Table of Contents
In addition to Git deployment, Heroku supports building and releasing apps via an API. The Heroku sbt plugin uses this API to provide direct deployment of prepackaged standalone web applications to Heroku.
In this article, you’ll learn how to include the Heroku sbt Plugin in your project, configure it, and deploy your application to Heroku.
Adding the plugin
To include the plugin in your project, add the following to your
addSbtPlugin("com.heroku" % "sbt-heroku" % "1.0.1")
If you’re not using Play, then you’ll also need to add the sbt-native-packager plugin. You can do this by adding the following to your
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.7.6")
sbt update to ensure the new dependencies are downloaded.
Next, we must configure the name of the Heroku application the plugin will deploy to. But first, create a new app. Install the Heroku CLI and run the
create command with the
-n flag, which will prevent it from adding a Git remote.
$ heroku create -n Creating obscure-sierra-7788... done, stack is cedar-14 http://obscure-sierra-7788.herokuapp.com/ | firstname.lastname@example.org:obscure-sierra-7788.git
Now add something like this to your
build.sbt, but replace “obscure-sierra-7788” with the name of the application you created.
herokuAppName in Compile := "obscure-sierra-7788"
You’re ready to deploy.
Deploying with the plugin
To create the slug and deploy it to Heroku, run the following command:
$ sbt stage deployHeroku ... [info] -----> Packaging application... [info] - app: obscure-sierra-7788 [info] - including: target/universal/stage/ [info] -----> Creating build... [info] - file: target/heroku/slug.tgz [info] - size: 30MB [info] -----> Uploading slug... (100%) [info] - success [info] -----> Deploying... [info] remote: [info] remote: -----> Fetching custom tar buildpack... done [info] remote: -----> JVM Common app detected [info] remote: -----> Installing OpenJDK 1.8... done [info] remote: -----> Discovering process types [info] remote: Procfile declares types -> console, web [info] remote: [info] remote: -----> Compressing... done, 78.9MB [info] remote: -----> Launching... done, v6 [info] remote: https://obscure-sierra-7788.herokuapp.com/ deployed to Heroku [info] remote: [info] -----> Done [success] Total time: 90 s, completed Aug 29, 2014 3:36:43 PM
Visit your application with this command:
$ heroku open -a obscure-sierra-7788
Or view the logs with this command:
$ heroku logs -a obscure-sierra-7788
The Heroku CLI will allow you to access the application, and run commands just like an application deployed with
The sbt plugin allows for several advanced configuration settings. All of these settings are defined in the
build.sbt file. For example, you may set the desired JDK version like so:
herokuJdkVersion in Compile := "1.8"
Valid values are 1.6, 1.7, and 1.8. The default is 1.8.
You can set configuration variables as a
herokuConfigVars in Compile := Map( "MY_VAR" -> "some value", "JAVA_OPTS" -> "-Xmx384m -Xss512k -XX:+UseCompressedOops" )
Be aware that any variable defined in
herokuConfigVars will override defaults, or previously defined config vars.
You may set process types (similar to a
herokuProcessTypes in Compile := Map( "web" -> "target/universal/stage/bin/my-app -Dhttp.port=$PORT", "worker" -> "java -jar target/universal/stage/lib/my-worker.jar" )
And finally, you can include additional directories in the slug like this (note that they must be relative to the project root):
By default, the plugin will package the essential directories under your project’s
target directory, so including additional directories should not be necessary in most cases.
Deploying to multiple applications
Most real applications will be required to deploy to multiple Heroku apps (such as dev, test, and prod). Normally this is done with multiple Git remotes. But with the plugin you can use either system properties, environment variables, or any other native sbt/Java configuration method. For example, you might define your
herokuAppName with a Map and choose a value with a system property like this:
herokuAppName in Compile := Map( "test" -> "your-heroku-app-test", "stage" -> "your-heroku-app-stage", "prod" -> "your-heroku-app-prod" ).getOrElse(sys.props("appEnv"), "your-heroku-app-dev")
Then run an sbt command similar to this:
$ sbt -DappEnv=test stage deployHeroku
The plugin has a few restrictions that must be considered.
It is required that your project use sbt 0.13.5 or greater.
You must use Java 1.7 or higher locally.
The plugin does not support Play 2.0 or 2.1.