In this Heroku tutorial, I’d like to share my experience about how to deploy a Java application on to Heroku cloud platform by putting a JAR file to Heroku, using Maven and Heroku CLI.
Why deploying JAR file?
Heroku provides several ways for deploying a Java application, so when using JAR deployment option?
Consider using JAR deployment if you’re in one of the following scenarios:
In this tutorial, I suppose that you already have a Java project, a Heroku account, and Heroku CLI and Maven installed on your computer.
Steps for deploying JAR file:
Now, let me walk you through each step in details.
Be sure that you declare the packaging type of your project in pom.xml file is jar:
<project ...>
...
<packaging>jar</packaging>
...
</project>
You can build and package your project to an executable JAR file in command prompt (terminal) by typing the following command (be sure that the current directory is your project’s root):
mvn package
In case you use Eclipse IDE, right-click on project and select Run As > Maven goal… and specify the goal package. The result is a JAR file created under target folder of the project.
Next, type heroku login to sign in your Heroku account in Heroku CLI. Then type the following command to install the Java Plugin for Heroku CLI:
heroku plugins:install java
You would see the output as follows:
Next, create a new app on Heroku by executing this command:
heroku create app_name --no-remote
The option --no-remote tells Heroku do not set Git remote reference for the project, as you’re going to deploy your app by sending JAR file, not by sending source code via Git push.
heroku deploy:jar target/your_app.jar -a app_name
You would see the output looks something like this:
That’s it. You’re all set. Your application has been successfully deployed on to Heroku using JAR deployment. Now you can access your application via URL https://app_name.herokuapp.com.
To watch the steps of deploying a JAR file to Heroku step by step, I recommend you watch the following video: