如何在现有的Spring Boot应用程序中更改嵌入式Tomcat的版本?

15

我目前正在运行内嵌的Tomcat的spring-boot version 1.4.0.RELEASE应用程序。包括的Tomcat版本为8.5.4

需要更新Tomcat版本到9.x。当我在mvnrepository这里查看时,发现有一个可用于Tomcat的更新版本9.0.5(如下图所示)

enter image description here

如果在pom.xml中没有直接提及此版本,我该如何在我的项目中使用它?

我不想采用传统的部署方式(将WAR构件部署到外部的Tomcat上)。


我怀疑它不会起作用,因为Tomcat已经进行了内部更改,而这些更改在Spring Boot 1.4的代码中没有反映。 - M. Deinum
好的。但是有没有任何方法可以更改那个版本呢?我准备将spring-boot版本升级到2.0.0.RELEASE,但这会带来Tomcat版本8.5.28 - shankulk
只需指定您想要使用的版本(假设您正在使用 spring-boot-starter-parent 作为项目的父类)。 - M. Deinum
4个回答

21

如果您正在使用Spring Boot Gradle Plugin和Spring Boot Starters,您可以通过在build.gradle中设置Maven项目属性来自定义版本。

ext['tomcat.version'] = '9.0.45'

对于Maven

<properties>
    <tomcat.version>9.0.45</tomcat.version>
<properties>

您可以在spring-boot-dependencies中找到所有可自定义的外部依赖。


1
很遗憾你没有提供完整的示例来说明如何做。这只是一个部分回答。 - Shrikant Prabhu
通过使用Gradle,可以通过以下方式实现:def tomcatVersion = '9.0.30'dependencies { implementation "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}" implementation "org.apache.tomcat.embed:tomcat-embed-el:${tomcatVersion}" implementation "org.apache.tomcat.embed:tomcat-embed-websocket:${tomcatVersion}" implementation "org.apache.tomcat:tomcat-annotations-api:${tomcatVersion}" } - Shrikant Prabhu
如果我们正在使用Maven,则应在<dependencyManagement>中添加依赖项,否则项目可能根本不会更改它。 - Felix Aballi
我不会以Tomcat 10为例,因为它已经被Jakartified,而当前的Spring库无法与其兼容。 - Piotr P. Karwasz
在较新版本的Spring Boot中,这是否仍然适用于Maven?看起来似乎不再存在pom.xml可以覆盖:https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-dependencies - kashev

19

我知道我来晚了,正在寻找类似的问题,想分享一个更字面的提示。

您需要覆盖在Spring父POM中设置的属性(这是必需的)以适应您的情况(还要考虑兼容性):

<properties>
......
    <tomcat.version>9.0.5</tomcat.version>
......
<properties>

这是根据[Spring简介][1]。他们的示例展示了许多其他精选依赖项。 [1]: http://www.springboottutorial.com/spring-boot-starter-parent


4

Maven项目

<properties>
  ...
  <tomcat.version>8.0.53</tomcat.version>
</properties>

<dependencyManagement>
        <dependencies>
         <!-- NON-INFORMATIVE -->
         <!--   <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>${bom.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>   -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-el</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

0

在build gradle中添加ext版本后更改版本并尝试运行应用程序时出现错误 获取错误:....嵌套异常是java.lang.NoSuchMethodError: org.apache.catalina.Context.addServletMapping(Ljava/lang/String)


2
目前你的回答不够清晰。请编辑并添加更多细节,以帮助其他人理解它如何回答所提出的问题。你可以在帮助中心找到有关如何撰写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接