Deploying a Spring Boot WAR to Wildfly, JBoss or Tomcat
This is a short guide on how to deploy a war-packaged Spring Boot application to Wildfly. As you know, Wildfly is the name of the community version of JBoss EAP, so this configuration should work for JBoss EAP as well.
Spring Boot guides encourage you to deploy your application with the embedded server when you package the jar file. The default server depends now on which web technology you use. If you include the dependency
spring-boot-starter-web, Spring Boot includes Tomcat by default. For WebFlux (
spring-boot-starter-webflux), Reactor Netty is the default embedded server.
Having a default embedded server is really powerful, but sometimes you need to deploy your application in an existing application server such as Wildfly, JBoss EAP or Tomcat, and for that, you need a deployable WAR file.
Preparing your Spring Boot app to deploy it to JBoss Wildfly
In order to make it work, you have to follow some easy steps.
- First, go to your application’s main class and make it extend
SpringBootServletInitializer. Note that, if you prefer to work with Spring Boot version 1.5 or earlier, this class was moved to a different package in Spring Boot 2:
org.springframework.boot.web.support.SpringBootServletInitializer: if you use Spring Boot 1.5
org.springframework.boot.web.servlet.support.SpringBootServletInitializer: if you use Spring Boot 2.0
- Override the
configuremethod and point your
sourcesto this same class (it’s the entry point of your application). Use the passed argument,
SpringApplicationBuilder. If you don’t do that, your different components won’t be detected, won’t be injected in the context so your application won’t work.
- Now you should write some additional configuration in your Maven
pom.xmlfile. Apart from changing the packaging method to war, you can exclude now the embedded tomcat server (you don’t need it) and specify a provided dependency with the
javax.servletAPI. On top of that, if you want to use Java 10, you need to explicitly create a manifest entry to include the
jdk.unsupportedmodule, since that’s needed by Spring Boot’s reflection. See this issue for more details. You can skip that plugin configuration if you’re not using Java 10.
- As you can see in the notes above, you can even remove the
spring-boot-maven-pluginplugin , since you don’t need it when deploying on the external server. This will save you some extra megabytes in the WAR file.
Note that you can also choose to specify the
spring-boot-starter-tomcat as provided, instead of excluding it from the starter web artifact. If you do that, you don’t need the
javax.servlet-api artifact to build your application. However, the alternative we follow within this post saves us some megabytes in the resulting WAR file.
Testing our Wildfly Spring Boot application with Docker
You can find a Dockerfile and a bash script with the sources. The Docker image is based on a custom one: mechero/wildfly. This image is actually inspired on the official one: jboss/wildfly, just modifying the base image to use the JDK 10 instead (the official one uses JDK 8 at the time of writing this post). Feel free of changing the base image to the official one if you’re fine with Java 8, I just wanted to show here the trickiest case so you have all the options available.
In the Dockerfile, what you do is just copying the deployable
war into the
deployments folder of the Wildfly server, which will take care of the rest and serve our application. To give it a friendlier name to the context, you can rename the file when copying it. Besides, we also want to expose the default web port of both Wildfly and our application, 8080.
Note that the
war file is supposed to be in the same folder as the
Dockerfile. The bash script included with the sources does that, but you can also copy it manually. Use the script to build your image or run this command in the terminal:
-t flag, you assign a tag to the docker image so it’s easier to identify. Now run it, exposing the port 8080 so it’s accessible from your host:
Then you can access our application’s REST API by navigating to this URL with your browser:
Any feedback or improvement suggestions? Do that via GitHub: https://github.com/mechero/spring-boot-jboss-wildfly