An improved version of this tutorial is available for my new framework, Javalin. Show me the improved tutorial

What is Heroku?

Heroku is a cloud application platform – a new way of building and deploying web apps. Our service lets app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling. — heroku.com

Heroku takes care of everything related to deployment, and gives you easy access to key commands via their tool Heroku Toolbelt. It’s very easy to get started with (as you’ll soon learn), and it provides a nice free-tier that you can use to deploy your webapps.

Initial Setup

Before we get started, there are a few things we need to do:

Configuring Maven

This is actually where most of the work is done. In order to easily deploy a Java application anywhere, you have to create a jar file containing your application and all of its dependencies. Open the pom.xml of your Spark Maven project and add the following configuration (below your dependencies tag):


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <!-- This tells Maven to include all dependencies -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Configuring Heroku

Before we can configure anything, we actually have to create a Heroku application. This can be done by using the heroku create command.
Open a terminal and navigate to your project root, then enter:


heroku create spark-heroku-example #choose your own application name 

Now that you have a Heroku application, we have to configure how to deploy it using Maven. This is pretty straightfoward using the Heroku Maven plugin.

We specify the JDK version and the app-name, along with the launch config:


<plugin>
    <groupId>com.heroku.sdk</groupId>
    <artifactId>heroku-maven-plugin</artifactId>
    <version>0.4.4</version>
    <configuration>
        <jdkVersion>1.8</jdkVersion>
        <!-- Use your own application name -->
        <appName>spark-heroku-example</appName> 
        <processTypes>
            <!-- Tell Heroku how to launch your application -->
            <!-- You might have to remove the ./ in front   -->
            <web>java -jar ./target/my-app-1.0-jar-with-dependencies.jar</web>
        </processTypes>
    </configuration>
</plugin>

When you’ve added the Heroku config to your pom, it should look like this.

Making Spark Listen on the Correct Port

The only thing left is making sure Spark can handle your requests. Heroku assigns your application a new port every time you deploy it, so we have to get this port and tell Spark to use it:


import static spark.Spark.*;

public class Main {

    public static void main(String[] args) {
        port(getHerokuAssignedPort());
        get("/hello", (req, res) -> "Hello Heroku World");
    }

    static int getHerokuAssignedPort() {
        ProcessBuilder processBuilder = new ProcessBuilder();
        if (processBuilder.environment().get("PORT") != null) {
            return Integer.parseInt(processBuilder.environment().get("PORT"));
        }
        return 4567; //return default port if heroku-port isn't set (i.e. on localhost)
    }

}

Now we can deploy our application using mvn heroku:deploy.

Again, make sure you are in your project root, then enter:


mvn heroku:deploy

That’s it. Our application is now avilable at https://spark-heroku-example.herokuapp.com/hello

The source code for this example can be found on GitHub.