spring-boot 替换内嵌tomcat版本

spring-boot中的内嵌tomcat有默认的指定版本,若想修改为其他版本,有以下两种途径:

使用parent的方式

若引入spring-boot的方式为加入<parent>:

1
2
3
4
5
6
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>

则只需在properties中指定tomcat版本即可:

1
2
3
<properties>
<tomcat.version>7.0.59</tomcat.version>
</properties>

这种方式比较简单,官方文档中也有说明

使用dependencyManagement的方式

有时我们的项目中有自己的parent,则需要使用<dependencyManagement>的方式引入spring-boot:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<properties>
<tomcat.version>7.0.59</tomcat.version>
</properties>
...
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

此时修改内嵌tomcat的版本则需要在<dependencyManagement>中加入内嵌tomcat的所有依赖,以替换默认的tomcat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<dependencyManagement>
<dependencies>
<!-- 替换spring-boot内嵌tomcat -->
<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>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

这种方式比较麻烦,但在不能引入parent和需要修改tomcat版本的情况下,也是无赖之举。

参考How to use Tomcat 8 + Spring Boot + Maven

-END-


------本文结束  感谢阅读------