Heroku Scala Support
Last updated November 28, 2022
Table of Contents
This document describes the general behavior of Heroku as it relates to the recognition and execution of Scala applications. For a more detailed explanation of how to deploy an application, see:
Getting Started with Scala on Heroku
Activation
Scala applications are recognized when files matching any of the following patterns are found:
/*.sbt
/project/*.scala
/project/build.properties
/.sbt/*.scala
When a deployed application is recognized as a Scala application, Heroku responds with -----> Scala app detected
.
$ git push heroku master
-----> Scala app detected
If an application also contains a /conf/application.conf
file, a Play 2.0 application is detected and Heroku responds with -----> Play 2.0 app detected
. Please see the Heroku Play Support article for more information.
Scala applications that use Maven can be deployed as well, but they will be treated as Java applications, so Heroku Java Support will apply.
Environment
The following environment variables will be set:
PATH
:.sbt_home/bin:/usr/local/bin:/usr/bin:/bin
PORT
: HTTP port to which the web process should bindDATABASE_URL
: URL of the Heroku Postgres database connection
Resizing dynos automatically changes Java memory settings. The JAVA_OPTS
config var can be manually adjusted to override these defaults.
When a Java process is started on your dyno, the follow Java options will automatically be picked up:
-
-Dfile.encoding=UTF-8
These options are configured as part of the environment variable JAVA_TOOL_OPTIONS
, which is intended to augment a command line in environments where the command-line cannot be accessed or modified. If you need to override these settings you can either define your preferred options in the Procfile
command (which will take precedence), or set your own JAVA_TOOL_OPTIONS
config var.
Adjusting Environment for a Dyno Size
When a new dyno size is selected, the following settings are automatically added to JAVA_TOOL_OPTIONS
:
eco
,basic
orstandard-1x
:-Xmx350m -Xss512k
standard-2x
:-Xmx768m
performance-m
:-Xmx2g
performance-l
:-Xmx12g
Monitoring Resource Usage
Additional JVM flags can be used to monitor resource usage in a dyno. The following flags are recommended for monitoring resource usage:
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+UseConcMarkSweepGC
See the troubleshooting article for more information about tuning a JVM process.
Build behavior
Applications must include a /project/build.properties
file with the sbt.version
property specifying a version of SBT between 0.11.0 and 1.x. SBT release candidates, betas and other pre-release versions are allowed.
The Heroku Scala buildpack will run sbt compile stage
to build the application. Applications must include a stage
task, which performs any tasks needed to prepare an application to be run in-place. For example, Typesafe’s sbt-native-packager
adds a stage
task to SBT that generates start scripts for an application. To use the plugin, create a /project/plugins.sbt
file containing:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.6")
Or you can write a custom stage
task by putting something like this in your build.sbt
:
val stage = taskKey[Unit]("Stage task")
val Stage = config("stage")
stage := {
(packageWar in Compile).value
(update in Stage).value.allFiles.foreach { f =>
if (f.getName.matches("webapp-runner-[0-9\\.]+.jar")) {
println("copying " + f.getName)
IO.copyFile(f, baseDirectory.value / "target" / "webapp-runner.jar")
}
}
}
You can test your task by running sbt compile stage
locally.
Clean builds
In some cases, builds need to clean artifacts before compiling. If a clean build is necessary, configure builds to perform clean
by setting SBT_CLEAN=true
:
$ heroku config:set SBT_CLEAN=true
Setting config vars and restarting example-app... done, v17
SBT_CLEAN: true
All subsequent deploys will use the clean
task. To remove the clean
task, unset SBT_CLEAN
:
$ heroku config:unset SBT_CLEAN
Unsetting SBT_CLEAN and restarting example-app... done, v18
Runtime behavior
By default, Scala applications are launched with a start script generated by the sbt-native-packager
:
web: target/universal/stage/bin/appname
If an application is not using the sbt-native-packager
or should be launched in a different way, a custom Procfile
can be included in the root of the project specifying a different entry for the web
process.
The Play framework automatically generates a start script for you, so no additional plugins are needed.
Supported JDK versions
Heroku currently uses OpenJDK 8 to run your application. OpenJDK version 7 is available. Depending on what major version you select the latest available update of that Java runtime will be used each time you deploy your app. For a list of current versions see the Java support article.
The JDK that your app uses will be included in the slug, which will affect your slug size.
Specifying a Java version
You can specify a Java version by adding a file called system.properties
to your application.
Set a property java.runtime.version
in the file:
java.runtime.version=1.8
1.8 is the default so if you’d like to use Java 8 you don’t need this file at all. You can specify JDK 7 by setting this property to “1.7”. For details on how to set specific update versions, see the Java support article.
Add-ons
A Heroku Postgres starter-tier database add-on is automatically provisioned for Scala applications. This populates the DATABASE_URL
environment variable.