从Gradle中的Spring Boot中排除Tomcat依赖项

Kre*_*sek 9 java spring tomcat gradle spring-boot

我正在使用Spring Boot和Jetty,我似乎无法排除Gradle构建文件中的所有Tomcat依赖项.

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"
}
Run Code Online (Sandbox Code Playgroud)

然而,当我运行gradle dependencies部分tomcat仍然存在,并导致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
...
Run Code Online (Sandbox Code Playgroud)

为什么不spring-boot-starter-tomcat排除spring-boot-starter-web

Kre*_*sek 18

啊哈,找到了原因.

我也compile("org.springframework.boot:spring-boot-starter-websocket")依赖于依赖spring-boot-starter-tomcat.Gradle依赖输出误导我认为这spring-boot-starter-web就是Tomcat仍然存在的原因.

我不得不添加以下内容:

compile("org.springframework.boot:spring-boot-starter-websocket") {
    exclude module: "spring-boot-starter-tomcat"
}
Run Code Online (Sandbox Code Playgroud)

获得的经验是,当您想要排除某些内容时,请仔细检查所有依赖项,以确保它从所有位置排除.并且可以改进gradle依赖项输出以减少误导性...


Geo*_*eno 15

我有同样的问题,所以除了spring-boot-starter-tomcat之外我还必须排除tomcat-embed-* jars,我是通过gradle配置完成的

configurations {
  compile.exclude module: 'spring-boot-starter-tomcat'
  compile.exclude group: 'org.apache.tomcat'
}
Run Code Online (Sandbox Code Playgroud)

  • 多谢,伙计!Undertow 在 Eclipse 中工作,但不在 jar 中,添加它,使其工作。对于那些使用较新 gradle 版本的人,不要将 compile 更改为 implementation。完全按照此处所写的方式使用。 (2认同)

小智 7

使用 Kotlin DSL,添加build.gradle.kts

configurations {
    implementation.configure {
       exclude(module = "spring-boot-starter-tomcat")
       exclude("org.apache.tomcat")
    }
}
Run Code Online (Sandbox Code Playgroud)


IPP*_*eek 6

梯度黑客

对于 Gradle 6,这对我有用,没有上面提到的模块排除:

configurations {
    compile.exclude module: 'spring-boot-starter-tomcat'
}
Run Code Online (Sandbox Code Playgroud)

插件配置

spring boot gradle 插件文档4.2.1 建议像这样声明提供的依赖项(假设您构建了一个战争):

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
Run Code Online (Sandbox Code Playgroud)

这些依赖项不会从战争中移除,而是转移到通常不会造成伤害的位置。

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
Run Code Online (Sandbox Code Playgroud)