By default, the spring boot applications are bundled into JAR regardless of its nature (web or stand alone app). Sometimes we need to create WAR file and deploy them in the tomcat server. In this article we are going to explore, how the spring boot application change in a way to create tomcat deployable WAR file.
In this application, maven will be used as the build and dependency management tool.
The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container. Typically, you update your application’s main class to extend SpringBootServletInitializer:
The next step is to update your build configuration so that your project produces a war file rather than a jar file. If you’re using Maven and using spring-boot-starter-parent (which configures Maven’s war plugin for you) all you need to do is to modify pom.xml to change the packaging to war:
The final step in the process is to ensure that the embedded servlet container doesn’t interfere with the servlet container to which the war file will be deployed. To do so, you need to mark the embedded servlet container dependency as provided.
Now you can use the below maven command to build the application.