This quarantine of 2020 has given me the chance to build my own blog using Java on the backend. The problem which I faced was deploying the app on a server. I've deployed my app on Heroku which gave me a free platform to deploy my own Java app along with a free MySQL database plugin.

In this article, I'll traverse the steps for deploying a Java app on Heroku using Git. For your convenience, I'll divide this article into three sections.

Section A

Step 1: Get a Heroku Account. You can go to this link for signing up if you don't have an account already.

Step 2: Create an app in the Heroku dashboard by clicking on New -> Create new app.

Heroku Dashboard

Give your app a name which is available from the server. You can choose the region according to your preference.

Step 3: Add a database to your app.

After creating your app, click on resources. On the search bar of Add-ons, type and search for MySQL. From the result list, add JawsDB MySQL. You can add any of the plugins from the list. Click on the added plugin and the server will automatically forward you to the settings of the database.

Step 4: Update your hibernate.cfg.xml using the connection info from the settings. Don't forget to set your hibernate dialect to

org.hibernate.dialect.MariaDB103Dialect

Run your app on local server to test the database.

Section B

Now that your app is connected to the database of the Heroku server, it's time to do some configurations on your app.

Step 1: Heroku assigns your application a new port every time you deploy it, so we have to get this port and tell your app to use it.

private static int getHerokuAssignedPort() {
    String herokuPort = System.getenv("PORT");
    if (herokuPort != null) {
      return Integer.parseInt(herokuPort);
    }
    return 7000;
}

Add above lines of code to your app and make necessary changes to receive and listen to the port.

Step 2: Update the pom.xml file with the maven-assembly-plugin.

To deploy your app, you need to have a jar file with your application and all of its dependencies. To get a jar file, you need to have a maven plugin declared in your app properties. Add these lines of code in your file.

<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>PackageName.MainClassName</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Step 3: Update the pom.xml file with heroku-maven-plugin.

After your jar file is created, you need to tell Heroku about how to launch our application. For that, we need to add another maven plugin. Add these lines of code to your pom.xml file for the plugin.

<plugin>
    <groupId>com.heroku.sdk</groupId>
    <artifactId>heroku-maven-plugin</artifactId>
    <version>1.1.3</version>
    <configuration>
        <jdkVersion>1.8</jdkVersion>
        <appName>yourAppNameInHeroku</appName>
        <processTypes>
            <!-- Tell Heroku how to launch your application -->
            <web>java -jar target/nameOfYourJarFile.jar</web>
        </processTypes>
    </configuration>
</plugin>

Step 4: Define a procfile

Heroku apps use a special plaintext file called the Procfile to explicitly declare what command should be executed to start your app.

The file name should be Procfile and it should have no extension. The procfile should look like this:

web: java -jar target/nameOfYourJarFile.jar

 

Section C

Now that your app is ready, it is finally the time to deploy your app. You can go the deploy tab of your app in Heroku and follow the steps there as well.

Step 1: Download and install Heroku CLI.

Step 2: Go to your app's root directory and open terminal. I'll be using Git Bash.

Step 3: Type and enter

$ heroku login

Heroku Login

After clicking any key but q, you'll be directed to a page in your default web browser to login. After logging in, the browser and the terminal, both should say that you are logged in after which you can close the browser.

Heroku logged in

Step 4: In the terminal, type and enter

$ git init
$ heroku git:remote -a yourAppName

command to Initialize a git repository in a new or existing directory of Heroku Git (you can also use Github, follow the steps on Deploy tab). Don't forget to replace yourAppName.

Step 5: After that, commit your code and deploy your application using the commands below.

$ git add .
$ git commit -am "yourCommitName"
$ git push heroku master

After you enter above commands, your code will be pushed to Heroku and on the terminal you see the app building up using the maven dependencies. 

If the app is built successfully without any error, it will run on Heroku server.

Deployed app

You can go to your app and click on open app to see your deployed app running successfully.

If you want your app to be directed to another domain, you'll have to do some DNS management. That's for another day. 

Hope you like it. Please, feel free to comment if you have some confusions.

 

Cover Photo Courtesy: VSchool