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:

  • You have a Java web app that needs to be run on a server. It’s not a stand-alone app with embedded server like Spring Boot apps.
  • You have only WAR file. No source code.
  • You don’t want to push your code to Heroku for deployment (for security reason).
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:

  • Build and package your project to WAR using Maven or IDE
  • Install Java Plugin for Heroku CLI
  • Create a new Heroku app
  • Use Heroku CLI to deploy WAR file
Now, let’s see each step in details.

 

1. Packaging Project to WAR File



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:

mvn package war

 

2. Install Java Plugin for Heroku CLI

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:

heroku install java plugin

 

3. Deploy WAR File to Heroku

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:

heroku deploy war command

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:

 

Other Heroku tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.