Deploying Executable JAR Files
Last updated 16 December 2019
Table of Contents
Many Java and JVM applications are packaged into a self-contained executable JAR file that includes application code, configuration and dependencies. These artifacts are often called “uberjars” or “fat jars”.
This article shows several different techniques for deploying a fat JAR to Heroku.
Using the Heroku Java CLI plugin
The Heroku Java CLI plugin can deploy a pre-built JAR file with very little set up or dependencies. To use it, you must have created a Heroku account and installed the Heroku CLI. Then run this command to install the plugin:
$ heroku plugins:install java
Then create an application:
$ heroku create --no-remote
And deploy your JAR file by running this command, replacing the app name (
sushi in this case) with the app name generated by the previous command:
$ heroku deploy:jar target/my-app.jar --app sushi
You can customize the execution of the JAR file by creating a
Procfile or including additional files in the deployment.
Using the Heroku Maven plugin
The Heroku Maven Plugin can be used as described in Deploying Java Applications with the Heroku Maven Plugin, but with the following configuration:
<includeTarget>false</includeTarget> <includes> <include>target/my-app-1.0.jar</include> </includes>
Then configure your process types to execute the JAR by adding some configuration like this:
<processTypes> <web>java -jar target/my-app-1.0.jar</web> </processTypes>
You can customize the deployment as described in Deploying Java Applications with the Heroku Maven Plugin.
Deployment with Git
When you deploy your source code to Heroku with
git push heroku master (as described in Deploying with Git), your JAR file will be included in your app’s slug. You only need to define how to run it in the app’s Procfile. For example:
web: java -jar target/myapp.jar
Make sure you add your
Procfile changes to your Git repository.
Deploying Clojure uberjars
The standard Clojure support will build an uberjar by default. If you wish to deploy a pre-built uberjar you can use the Leiningen Heroku plugin.
Deploying Scala fat jars
If you are packaging your application with sbt-assembly, you can deploy that file by adding the following configuration to your build.sbt:
herokuFatJar in Compile := Some((assemblyOutputPath in assembly).value)
If not using sbt-assembly, you may replace
(assemblyOutputPath in assembly).value with the relative path to your JAR file. Then you can deploy by running:
$ sbt assembly deployHeroku
The sbt-heroku plugin will skip the default sbt-native-packager execution and deploy your JAR directly to Heroku.
Deployment with Scala, Clojure and Gradle
Other JVM languages have support for deploying executable JAR files on Heroku. For more information see:
See Deploying Java Applications to Heroku from Eclipse or IntelliJ IDEA to learn how to deploy from Eclipse, IntelliJ or a CI Server.