Deploying a WAR to an application server in Spring Boot

How can we package our application as a WAR file and not a JAR?

In this tutorial, we are taking a look at a student’s question from my Spring Boot Introduction Course. We might not realize it but when we start a new project we are selecting how we want to package this application when we ready to go live. 

Spring Boot: Package as WAR

I recently received the following question from a student: 

In the real world, an application will be deployed to an application server like JBoss. How can the application be wrapped into a war file and deploy to the application server?

Thank you for the question and now let’s dive into the answer. 

WAR vs JAR

The first thing we need to discuss is what is the difference between a WAR and a JAR. WAR stands for Web Application Archive and is deployed on a Servlet Container like Tomcat or Jetty. JAR stands for Java ARchive and this will contain and embedded servlet container like Tomcat. These are two very different ways of deploying an application but I can assure that in the “real world” deploying a JAR file is very common. 

Selecting your Package Type

When you create a new project using the Spring Initializr you have the option to select how you want to package your application. 

Spring Boot Package Type

 

Converting a Spring Boot JAR Application to a WAR

We don’t always get the opportunity to create a new project from scratch, so what happens when you’re working on an existing project? If you have a Maven based project that is already using JAR as its packaging type you might have a POM that looks like this. 

We can use 3 very simple steps to convert our application to use WAR as the package type. 

Step 1: Adding the Servlet Initializer Class

In an application where we are using JAR as the package type, we only need the main application class that looks like this. 

In our WAR deployment, we are going to add another class that will help configure our web application. We are going to add a new class called Servlet Initializer that looks like this. You could also extend your main application class if you like and override configure in that class but I like separating this out. 

In my example, I am using my main application class (WarnotjarApplication.class) as an argument to the sources method. You should end up with the main application class and a class called ServletInitializer. 

WAR not JAR

 

Step 2: Update the Tomcat Dependency

In our Spring Boot Starter Web dependency, there is a Tomcat dependency declared. We can’t go in and make a change there but we can make the change in our POM. We need to change that dependency to say that this will be provided for us. 

Step 3: Change the package type to war

Packaging your application

With those changes in place, you can now package your application. 

Maven Package WAR

If you look inside of the /target folder now you should see the war file. You can now take that file and deploy on your servlet container. 

Spring Boot WAR Screencast

Conclusion

Spring Boot makes it easy for us to package our applications to fit our needs. I hope this tutorial showed how easy it was to convert an existing JAR application over to a WAR. 

Question: What Spring Boot Deployment issues are you facing? 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.