Spring Boot is an opinionated convention-over-configuration framework for creating stand-alone Spring-based Java applications.
Its objective is to provide you with a preselected best practice choice of libraries (both from the Spring platform and by third parties) for a given task, e.g.
- building a web application that connects to a database and provides REST resources
- creating a scheduled batch task that exports and aggregates data from various data sources
- providing an API that connects to SOAP Web Services
It both helps you with getting started quickly and allows you to go about application development in a pattern-like manner that makes application behaviour more predictable and easier to understand.
In keeping with that stand-alone, cookie cutter approach the usual way Spring Boot applications are deployed is as a JAR file (including an embedded web container such as Tomcat if your application is a web app). This allows you to run your application right away without any configuration hassle. In addition to that, this self-contained approach is expedient to the microservices design pattern, which has made Spring Boot a prime choice for modern microservice architectures.
Sometimes however for instance due to existing infrastructure that can’t be changed quickly it might be necessary to deploy applications – web applications in particular – the old-fashioned way as WAR (Web application ARchive) files in existing servlet containers or application servers.
In order to do so you only have to make a few changes to your application configuration (as described in the Spring Boot documentation part on traditional deployment):
- Provide a SpringBootServletInitializer and configure() method:123456789101112131415@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);}public static void main(String[] args) throws Exception {SpringApplication.run(Application.class, args);}}
- Change the packaging option in the application’s pom.xml to war:123<packaging>war</packaging>
- Finally, tell Maven to not use the provided embedded Tomcat servlet container in production in order to not interfere with the existing servlet container you deploy the WAR file to:123456789<dependencies><dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-tomcat</artifactid><scope>provided</scope></dependency></dependencies>
If you’d like to know more about Spring Boot application deployment in general and converting Spring Boot applications from JAR to WAR deployments in particular please have a look at these posts on the Spring website:
- Deploying Spring Boot Applications
- Converting a Spring Boot JAR Application to a WAR (including directions for using Gradle instead of Maven, which I omitted in this article for the sake of brevity)