Deploying a tools.deps Clojure project to Heroku

Recently the offical Clojure-buildpack on Heroku added support for Clojure's CLI tools.

This is some great news as it opens the door to hosting tools.deps Clojure projects on Heroku - no Leiningen required.

Though as the Clojure-buildpack still assumes a Leiningen-project, some small tweaks are needed when using tools.deps.

Let's dive in!

two-step

There are two things we need to take care of in order to deploy a tools.deps project:

  1. ensure the project is seen as a Clojure-project
  2. override the build-step

quack like Leiningen

In order for our project to be build by the Clojure-buildpack, we need to make sure it 'looks like' a Leiningen-project.
We accomplish this by adding an empty file project.clj to the root of our project.

building

Nextup is the actual build-step. By default this will invoke Leiningen to build an uberjar. We can override this by adding an executable script bin/build with the tools.deps equivalent of lein uberjar.

There're several tools for creating uberjars with tools.deps. For this example we'll use depstar and add the following alias to deps.edn:

;; deps.edn
{
 ...
 :aliases {:depstar
              {:extra-deps
                 {seancorfield/depstar {:mvn/version "0.1.5"}}}}
}

Then in bin/build we provide the command to create our application's uberjar:

#!/usr/bin/env bash
clojure -A:depstar -m hf.depstar.uberjar foo.jar

Note:

  • make sure to use clojure and not clj as the latter will trigger an error (ie Please install rlwrap for command editing or use "clojure" instead.)
  • don't forget to chmod +x bin/build or else the file is ignored
  • if you need a specific version of the Clojure CLI tools, set the config variable CLOJURE_CLI_VERSION with one of the versions listed here1.

As a final step, make sure the Procfile contains the command to start the application:

web: java -cp foo.jar clojure.main -m foo.core

That's it! You're one git push heroku master away from deploying your tools.deps project to Heroku.

resources

  1. This CHANGELOG will tell you what changed between CLI versions