Deploy Spring Boot Apps From Jar to War
In this article, see how to deploy Spring Boot apps from jar to war.
Join the DZone community and get the full member experience.
Join For FreeMake it Platform-Independent
Developing Spring Boot applications could be easy these days with the likes of annotations and initializes as Maven, Spring Boot, and embedded servers.
So as we know when building a Spring Boot application, by default, we package the application into a JAR file and execute our main application class into a main embedded tomcat server. We then run our application tests, web app, or REST endpoints within our environment for easy testing and debugging.
What if we want to make our application server independent and drop our application on other servers like Weblogic, Jboss, Wildfly, etc.?
In order to do this, we have to make our application a WAR file.
Here are simple steps to do so:
1. Update your maven pom.xml with the following changes
Set your packaging tag to war
<packaging>war</packaging>
Set all Tomcat Jar files to provided
x
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Then, we need to update the maven-war-plugin
, not to fail if web.xml
is missing. This can be done by updating the plugin information in the build
tag and by removing the org.springframework.boot
plugin in the pom as shown below:
xxxxxxxxxx
<plugins>
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>default-war</id>
<phase>prepare-package</phase>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
2. Update your Main class to extends SpringBootServletInitializer
xxxxxxxxxx
public class SpringBootWarDeploymentApplication extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWarDeploymentApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootWarDeploymentApplication.class, args);
}
}
3. Rename your file SpringBootWarDeployment-0.0.1.war to your desired name springrestapi.war
4. Check if your Server container has missing web.xml file capabilities
We generally want version numbers out of our build files as certain containers fails to deploy dash versions.
Conclusion
This worked for me, generally, I downloaded Apache Tomcat 8.5 and edited the pom.xml plugin and viola! It worked. All my projects ended up working because Tomcat 8.5 and up accepted no Web.xml and the exclusion thereof.
Have fun, folks!
Don't forget to like, comment, and share!
Opinions expressed by DZone contributors are their own.
Comments