Java web applications are usually deployed as WAR (Web Archive) files on a server like Apache Tomcat. In this Heroku tutorial, you will learn how to deploy a WAR file to Heroku cloud platform, using Maven and Heroku CLI. Heroku runs a WAR-deployed app using an instance of Apache Tomcat called Webapp Runner.
Why deploying WAR File?
Heroku provides several ways for deploying Java apps on its platform. So when choosing WAR deployment over others? Well, consider using WAR deployment if:
To follow this guide, you must have a Heroku account; and have Maven and Heroku CLI installed on your computer.
Steps for deploying a WAR File to Heroku:
Now, let’s see each step in details.
Open the project’s pom.xml file to make sure that the packaging type is war:
<project ...>
...
<packaging>war</packaging>
...
</project>
Then type the following command to build the project and package to a WAR file:
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 WAR file created under targetfolder of the project:
Next, type heroku loginto 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-remotetells 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:war target/your_app.jar -a app_name
You would see the output looks something like this:
Now you can type the following command to access the newly deployed app in your web browser:
heroku open -a app_name
Note that Heroku runs a WAR-deployed app under an instance of Apache Tomcat called Webapp Runner. You can type the following command to learn more about WAR deployment options (e.g. how to change version of Tomcat):
heroku deploy:war --help
That’s how to deploy a WAR file to Heroku cloud platform. To see the steps in action, I recommend you watch the following video: