Use tomcat embedded with custom versions
18 Jun 2019 | tags: Java Tomcat Spring BootRationale TL;DR
I’ve been trying to shp a Java application as an executable WAR file (yes, I know about jar files, but I don’t want to skip one final monster). To do so I need to embed Tomcat on the WAR file.
Using Spring Boot starter web
Usually that would be as easy as using the spring-boot-starter-web artifact. It will pull the three tomcat-embed-core, tomcat-embed-el and tomcat-embed-websocket. So far so good. The issue here is that those will be in sync with the Spring Boot you’re using, so what if you’re still on 1.5.x but you want to use Tomcat 9.x? Or what if you’re already on SB 2.x but you want Tomcat 8.5.x?
Selecting the Tomcat version to run
You need to change your pom file to include the following:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>${tomcat.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>${tomcat.version}</version>
<scope>compile</scope>
</dependency>
That would pull those dependencies from the maven central repository. Note that you need to define the property ${tomcat.version} somewhere in the properties block in your pom file.