Blog

Packaging and deploying Spring Boot applications as WAR files

By  
Alejandro Duarte
Alejandro Duarte
·
On Sep 2, 2021 4:28:59 PM
·

This guide explains how to:

Packaging a Maven-based Spring Boot application as a WAR file

To change the packaging in a Maven-based Spring Boot application that was generated using the Spring Initializr or Vaadin Start tools, make the following changes in the pom.xml file:

1. Follow the official Spring Boot docs how to do traditional war packaging, instead of jar (the default). You should at least change the packaging in your pom.xml or build.gradle file, modify your application class to do servlet init and change Tomcat (or other server starter) to be in provided scope.

2. Optional: If you want to simplify the name of the WAR file and always build a file with the same name without version numbers, add the following to the <build> section:

<finalName>${project.artifactId}</finalName>

4. Build and package the application by running the mvn package command. If you are using Vaadin, enable the production profile (mvn package -P production). You can find the WAR file in the target/ directory inside the Maven project.

Deploying a WAR file to Apache Tomcat

To deploy the application configured in the previous section to a local instance of Apache Tomcat:

  1. Download Apache Tomcat from the project website. Make sure to download the correct version, depending on the Java and Servlet API versions that your application uses. For example, in the case of Vaadin applications, download Apache Tomcat version 9.
  2. Extract the downloaded file.
  3. Start the server by running the start.sh or start.bat scripts that you can find in the bin/ directory inside the Apache Tomcat installation directory. You might have to add execute permissions to the script files in the bin/ directory. For example, in Unix-like operating systems, run chmod +x bin/*.sh.
  4. Copy the WAR file from the target/ directory inside your Maven project to the webapps/ directory inside the Apache Tomcat installation directory.
  5. The application should be deployed automatically and made available at http://localhost:8080/your-war-file-name. Use ROOT.war if you want to deploy to the context root (http://localhost:8080/).

Deploying a WAR file to Eclipse Jetty

To deploy the application previously configured to a local instance of Eclipse Jetty:

  1. Download Eclipse Jetty from the project website. Make sure to download the correct version, depending on the Java and Servlet API versions that your application uses. For example, in the case of Vaadin applications, download Eclipse Jetty 9.
  2. Extract the downloaded file.
  3. If you downloaded Eclipse Jetty 10, configure the server by running java -jar start.jar --add-module=server,http,deploy inside the Eclipse Jetty installation directory.
  4. Start the server by running start.sh start in the bin/ directory inside the Eclipse Jetty installation directory. You might have to add execute permissions to the script files in the bin/ directory by running chmod +x bin/*.sh.
  5. Copy the WAR file from the target/ directory inside your Maven project to the webapps/ directory inside the Eclipse Jetty installation directory.
  6. The application should be deployed automatically and made available at http://localhost:8080/your-war-file-name. Use root.war if you want to deploy to the context root (http://localhost:8080/).
Alejandro Duarte
Alejandro Duarte
Software Engineer and Developer Advocate at MariaDB Corporation. Author of Practical Vaadin (Apress), Data-Centric Applications with Vaadin 8 (Packt), and Vaadin 7 UI Design by Example (Packt). Passionate about software development with Java and open-source technologies. Contact him on Twitter @alejandro_du or through his personal blog at www.programmingbrain.com.
Other posts by Alejandro Duarte