java 在 Gradle 中从 Spring Boot 中排除 Tomcat 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42275516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:30:09  来源:igfitidea点击:

Excluding Tomcat dependencies from Spring Boot in Gradle

javaspringtomcatgradlespring-boot

提问by Kre?imir Nesek

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

我正在将 Spring Boot 与 Jetty 一起使用,但似乎无法排除 Gradle 构建文件中的所有 Tomcat 依赖项。

Relevant part of build.gradle:

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 dependenciesparts of tomcat are still there, and causing issues with WebSockets:

然而,当我运行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.Hymanson.core:Hymanson-databind:2.8.3
|    |    +--- com.fasterxml.Hymanson.core:Hymanson-annotations:2.8.0 -> 2.8.3
|    |    \--- com.fasterxml.Hymanson.core:Hymanson-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-tomcatexcluded from spring-boot-starter-web?

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

回答by Kre?imir Nesek

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-webis the reason why Tomcat was still there.

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

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

经验教训是,当您想排除某些内容时,请仔细检查所有依赖项,以确保将其从所有位置排除。并且可以改进 gradle 依赖项输出以减少误导...

回答by George Otieno

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

我有同样的问题,所以除了排除spring-boot-starter-tomcat我还必须排除tomcat-embed-*jars,我通过gradle 配置做到了这一点

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

回答by IPP Nerd

gradle hack

gradle 黑客

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

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

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

plugin config

插件配置

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

春天的Gradle启动插件文档4.2.1推荐申报提供的依赖性就像这样(假设你建一个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.

依赖项不会从War中移除,而是移动到它们通常不会造成伤害的位置。

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