How to Deploy WAR File to Heroku
- Details
- Written by Nam Ha Minh
- Last Updated on 29 October 2021   |   Print Email
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:
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:
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:
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:
- What is Heroku for Developers (Benefits, how it works and key concepts)
- Deploy Simple Spring Boot Project to Heroku using Git and Heroku CLI
- Change Java version for Apps deployed on Heroku
- Add Custom Domain Names for Heroku Apps
- Deploy Spring Boot App with MySQL Database on Heroku
- How to Enable Secure Connection for Heroku Apps
- How to Deploy JAR File to Heroku