The default packaging for Spring boot applications is jar which contains both the business classes and the embedded Web server. So why bothering creating a WAR archive ? this is mostly to deploy the Spring Boot application on other containers such as WildFly. In this case, all you need to do is declaring the packaging type ‘war’ in pom.xml
file.
<project> <packaging>war</packaging> </project >
The next thing we want to do is setting the scope of the embedded Web server (by default tomcat) to “provided“. Why? because we don’t want to ship the emdedded Web server as part of the WAR, but we might still use it in the development lifecycle. Here is a sample which uses all the required settings to produce a war and set Tomcat dependencies as provided:
<?xml version="1.0" encoding="UTF-8"?><project> <groupId>com.masterspringboot</groupId> <artifactId>webdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springbootdemo</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-hateoas</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>repository.spring.release</id> <name>Spring GA Repository</name> <url>http://repo.spring.io/release</url> </repository> </repositories> </project>
Finally, if we are going to deploy our application on Tomcat, we need to iinitialize the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface::
@SpringBootApplication public class SpringBootTomcatApplication extends SpringBootServletInitializer {}
To build our WAR application, we execute:
mvn clean install
After that, the WAR file will be created in the target folder of your project. Mind it that this new configuration makes our Spring Boot application a non-standalone application (if you would like to have it working in standalone mode again, remove the provided scope from the tomcat dependency).
Deploying a Spring Boot Application on WildFly
If you want to learn how to deploy a Spring Boot application on WildFly, check this tutorial: Deploying a Spring Boot application on WildFly
Found the article helpful? if so please follow us on Socials