简体   繁体   中英

Excluding Tomcat dependencies from Spring Boot in Gradle

I'm using Spring Boot with Jetty and it seems I cannot exclude all the Tomcat dependencies in my Gradle build file.

Relevant part of build.gradle:

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

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

Yet when I run gradle dependencies parts of tomcat are still there, and causing issues with WebSockets:

...
|    
+--- org.springframework.boot:spring-boot-starter-web: -> 1.4.1.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.4.1.RELEASE (*)
|    +--- org.hibernate:hibernate-validator:5.2.4.Final
|    |    +--- javax.validation:validation-api:1.1.0.Final
|    |    +--- org.jboss.logging:jboss-logging:3.2.1.Final -> 3.3.0.Final
|    |    \--- com.fasterxml:classmate:1.1.0 -> 1.3.1
|    +--- com.fasterxml.jackson.core:jackson-databind:2.8.3
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0 -> 2.8.3
|    |    \--- com.fasterxml.jackson.core:jackson-core:2.8.3
|    +--- org.springframework:spring-web:4.3.3.RELEASE
|    |    +--- org.springframework:spring-aop:4.3.3.RELEASE (*)
|    |    +--- org.springframework:spring-beans:4.3.3.RELEASE (*)
|    |    +--- org.springframework:spring-context:4.3.3.RELEASE (*)
|    |    \--- org.springframework:spring-core:4.3.3.RELEASE
|    +--- org.springframework:spring-webmvc:4.3.3.RELEASE
|    |    +--- org.springframework:spring-aop:4.3.3.RELEASE (*)
|    |    +--- org.springframework:spring-beans:4.3.3.RELEASE (*)
|    |    +--- org.springframework:spring-context:4.3.3.RELEASE (*)
|    |    +--- org.springframework:spring-core:4.3.3.RELEASE
|    |    +--- org.springframework:spring-expression:4.3.3.RELEASE (*)
|    |    \--- org.springframework:spring-web:4.3.3.RELEASE (*)
|    \--- org.springframework.boot:spring-boot-starter-tomcat:1.4.1.RELEASE
|         +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.5
|         +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.5
|         \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.5
|              \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.5
...

Why isn't spring-boot-starter-tomcat excluded from spring-boot-starter-web ?

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'
}

With the kotlin gradle dsl:

configurations {
    implementation.configure {
       exclude(module = "spring-boot-starter-tomcat")
       exclude("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

Answer from Krešimir Nesek is inspired me to solve my bug. But I solve it in a little different way.

My problem is invoked by including other module with Spring-Boot from a module built upon WebFlux and springCoroutines in Kotlin.

My way in Kotlin is :

 exclude("org.springframework.boot", "spring-boot-starter-tomcat")

The first argument "org.springframework.boot" is for group, the second one is for specific module.

The complete example of the dependency (Kotlin DSL):

implementation(group = "org.springframework.boot", name = "spring-boot-starter-web") {
   exclude(module = "spring-boot-starter-tomcat")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM