Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 893
Krešimir Nesek Avatar asked Feb 16 '17 13:02

Krešimir Nesek


People also ask

How do we exclude the embedded Tomcat server from spring boot application?

If we want to exclude tomcat from spring boot, we don't need to do much, we just need to add one additional block(<exclusions>) to the Spring Boot dependency. <exclusions> tag is used to make us sure that given server/artifactId is being removed at the time of build.

How do you avoid transitive dependencies in Gradle?

When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com.

Can we disable embedded server in spring boot?

Using Dependencies The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies. This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file.


1 Answers

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...

like image 90
Krešimir Nesek Avatar answered Sep 20 '22 06:09

Krešimir Nesek