Excluding Tomcat dependencies from Spring Boot in Gradle

With the kotlin gradle dsl:

configurations {
    implementation.configure {
       exclude(module = "spring-boot-starter-tomcat")
       exclude("org.apache.tomcat")
    }
}


Aha, found the reason.

I also had compile("org.springframework.boot:spring-boot-starter-websocket") dependency that was also depending on spring-boot-starter-tomcat. Gradle dependency output mislead me into thinking that spring-boot-starter-web is the reason why Tomcat was still there.

I had to add the following:

compile("org.springframework.boot:spring-boot-starter-websocket") {
    exclude module: "spring-boot-starter-tomcat"
}

Lesson learned is that when you want to exclude something, double-check all of your dependencies to make sure it is excluded from all the places. And gradle dependencies output could be improved to make it less misleading...


I had the same problem, so on top of excluding spring-boot-starter-tomcat I also had to exclude tomcat-embed-* jars, I did this through gradle configurations

configurations {
  compile.exclude module: 'spring-boot-starter-tomcat'
  compile.exclude group: 'org.apache.tomcat'
}

gradle hack

With Gradle 6 this worked for me without the module exclusion mentioned above:

configurations {
    compile.exclude module: 'spring-boot-starter-tomcat'
}

plugin config

The spring boot gradle plugin documentation 4.2.1 recommends to declare provided dependencies like this (assuming you build a war):

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

The dependencies won't be removed from the war but moved to a location where they usually don't harm.

WEB-INF/lib-provided/spring-boot-starter-tomcat-2.2.4.RELEASE.jar
WEB-INF/lib-provided/tomcat-embed-websocket-9.0.30.jar
WEB-INF/lib-provided/tomcat-embed-core-9.0.30.jar
WEB-INF/lib-provided/tomcat-embed-el-9.0.30.jar