Continuous Delivery (or Continuous Deployment, or CD) is a software engineering approach in which software functionalities are delivered frequently through automated deployments.
We will be using Heroku, a platform as a service (PaaS) based on a managed container system, with integrated data services, for deploying and running software applications. Heroku is primed for continuous delivery.1 It is, in a nutshell, a cloud application platform that lets you deploy your server online. By taking care of most things related to deployment, it makes it easy to get your application up and running.
Deploying SparkJava on Heroku
Before we get started, you must:
Build a simple SparkJava App
Create a new Gradle Java project
herokuDemo in IntelliJ and add the following dependencies to it
Create the following Java class:
You can run this application and point your browser to
http://localhost:7000/ to see the
Hi Heroku! message!
Prepare to deploy
We want to package our SparkJava application as a single executable file that can be run on Heroku. We are going to make a fat
JAR file2 for this purpose. A fat JAR contains your application and all the dependencies needed to run your application.
Gradle is able to package your Java application into a JAR file but, by default, it does so without including the project dependencies.
We can overwrite the default behavior by adding a few lines to the project's
Now you can run Gradle's
jar task: open the terminal at the root directory of your Java project and type the following command:3
Once the process is finished, you can find your newly packaged JAR file in the
build/libs directory. Verify that the archive is valid by running the following command:
Heroku Gradle Plugin
To deploy Gradle based JVM applications directly to Heroku, we will use a heroku-gradle plugin. Add the plugin to your
Moreover, you need to add a configuration for the heroku-gradle plugin; add the following to your
Making SparkJava Listen on the Correct Port
When we deployed SparkJava locally, we used the
7000 port on
localhost. Heroku assigns your application a new port every time you deploy it, so we have to get this port and tell SparkJava to use it:
A single app runs in different environments, for example on your development machine when developing the app and on the cloud (e.g. Heroku) in production. Although you run the same exact code, but the environments usually (almost always!) have their own environment-specific configurations. An app’s environment-specific configuration can be stored in environment variables (and not in the app’s source code). In the above code, we get the value of
PORT env variable since Heroku automatically sets that env variable value when running an app. Thus, we can use that to decide if the app is currently running on Heroku or not.
Heroku & Git
Heroku is deeply integrated with the Git workflow. However,
heroku-gradle plugin that we listed under
plugins is used to deploy Gradle-based JVM applications directly to Heroku without pushing to a Git repository.
Before deployment, you must create a Heroku app using the Heroku CLI; open the terminal at the root directory of your Java project and type the following command:
It will allow you to login to your Heroku account (via your browser if you want). Next, try:
This will create a new Heroku app with a random unique name (e.g. boiling-stream-95315) for your application.
You "create" the app once, before deploying for the first time.
You must have Heroku CLI installed in order for these commands to work!
Next, open your
build.gradle file and add the following under
Note that you must replace the above using whatever name Heroku has assigned to your app.
Then, run the following command:4
If all goes well, you will get a message that looks like this
Note when deploying, it may ask you a yes/no question (as indicated above): you need to say
yes to that!
You can point your browser to the URL of your Heroku application. For example, the example above is deployed at https://boiling-stream-95315.herokuapp.com/.
- Read more about Continuous Delivery on Heroku at https://www.heroku.com/continuous-delivery.↩
- JAR stands for Java ARchive. It is used for aggregating many Java files into one. It is the preferred way to bundle a Java application. Read more on Oracle's website.↩
- You can also run this task in IntelliJ from the Gradle Tool Window↩
- You can configure IntelliJ to run this task by following the instructions here.↩