Skip to content

Commit 2685f4b

Browse files
committed
Change tomcat and jetty runtime modules to starters
Change `spring-boot-tomcat-runtime` and `spring-boot-jetty-runtime` into starter POMs and reduce the number of dependencies needed for `spring-boot-tomcat` and `spring-boot-jetty`. The runtime starters provide only the jars required to run the embedded server along with the module jar itself (excluding transitive dependencies) and `spring-boot-webserver` (excluding transitive dependencies). The build setup required for an executable jar is slightly different between Maven and Gradle. For Maven, the regular module is put in the `provided` scope. For Gradle, the regular module remains in main configuration and the runtime jar is put in the `providedRuntime` configuration. The reference documentation has been updated to show how to configure things if starters are being used. Manual testing has been performed to ensure that wars build with Maven and Gradle work with both Tomcat and Jetty in both deployed and `java -jar` modes. Closes gh-48175
1 parent 2e75008 commit 2685f4b

File tree

18 files changed

+128
-57
lines changed

18 files changed

+128
-57
lines changed

build-plugin/spring-boot-gradle-plugin/src/docs/antora/modules/gradle-plugin/examples/packaging/war-container-dependency.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ apply plugin: 'io.spring.dependency-management'
2323

2424
// tag::dependencies[]
2525
dependencies {
26-
implementation('org.springframework.boot:spring-boot-starter-web')
27-
providedRuntime('org.springframework.boot:spring-boot-tomcat-runtime')
26+
implementation('org.springframework.boot:spring-boot-starter-webmvc')
27+
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat-runtime')
2828
}
2929
// end::dependencies[]

build-plugin/spring-boot-gradle-plugin/src/docs/antora/modules/gradle-plugin/examples/packaging/war-container-dependency.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ apply(plugin = "io.spring.dependency-management")
88
// tag::dependencies[]
99
dependencies {
1010
implementation("org.springframework.boot:spring-boot-starter-web")
11-
providedRuntime("org.springframework.boot:spring-boot-tomcat-runtime")
11+
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat-runtime")
1212
}
1313
// end::dependencies[]

build-plugin/spring-boot-gradle-plugin/src/docs/antora/modules/gradle-plugin/pages/packaging.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The `assemble` task is automatically configured to depend upon the `bootWar` tas
2727
=== Packaging Executable and Deployable Wars
2828

2929
A war file can be packaged such that it can be executed using `java -jar` and deployed to an external container.
30-
To do so, the embedded servlet runtime should be added to the `providedRuntime` configuration, for example:
30+
To do so, the embedded servlet container runtime should be added to the `providedRuntime` configuration, for example:
3131

3232
[tabs]
3333
======
@@ -45,7 +45,7 @@ include::example$packaging/war-container-dependency.gradle.kts[tags=dependencies
4545
----
4646
======
4747

48-
This ensures that the runtime is packaged in the war file's `WEB-INF/lib-provided` directory from where it will not conflict with the external container's own classes.
48+
This ensures that the runtime jars are packaged in the war file's `WEB-INF/lib-provided` directory from where they will not conflict with the external container's own classes.
4949

5050
NOTE: `providedRuntime` is preferred to Gradle's `compileOnly` configuration as, among other limitations, `compileOnly` dependencies are not on the test classpath so any web-based integration tests will fail.
5151

documentation/spring-boot-docs/src/docs/antora/modules/how-to/pages/deployment/traditional-deployment.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,31 @@ apply plugin: 'war'
3333
----
3434

3535
The final step in the process is to ensure that the embedded servlet container does not interfere with the servlet container to which the war file is deployed.
36-
To do so, you need to mark the embedded servlet runtime dependency as being provided.
3736

38-
If you use Maven, the following example marks the servlet runtime (Tomcat, in this case) as being provided:
37+
For Maven, you need to mark the embedded servlet container dependency as being `provided`.
38+
For example:
3939

4040
[source,xml]
4141
----
4242
<dependencies>
4343
<!-- ... -->
4444
<dependency>
4545
<groupId>org.springframework.boot</groupId>
46-
<artifactId>spring-boot-tomcat-runtime</artifactId>
46+
<artifactId>spring-boot-starter-tomcat</artifactId>
4747
<scope>provided</scope>
4848
</dependency>
4949
<!-- ... -->
5050
</dependencies>
5151
----
5252

53-
If you use Gradle, the following example marks the servlet runtime (Tomcat, in this case) as being provided:
53+
If you use Gradle, you need to move only the runtime dependencies into the `providedRuntime` configuration.
54+
For example:
5455

5556
[source,gradle]
5657
----
5758
dependencies {
5859
// ...
59-
providedRuntime 'org.springframework.boot:spring-boot-tomcat-runtime'
60+
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat-runtime'
6061
// ...
6162
}
6263
----

documentation/spring-boot-docs/src/docs/antora/modules/how-to/pages/webserver.adoc

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ Many Spring Boot starters include default embedded containers.
1818
When switching to a different HTTP server, you need to swap the default dependencies for those that you need instead.
1919
To help with this process, Spring Boot provides a separate starter for each of the supported HTTP servers.
2020

21-
The following Maven example shows how to exclude Tomcat and include Jetty for Spring MVC:
21+
The following example shows how to exclude Tomcat and include Jetty for Spring MVC:
2222

23+
[tabs]
24+
======
25+
26+
Maven::
27+
+
2328
[source,xml]
2429
----
2530
<dependency>
2631
<groupId>org.springframework.boot</groupId>
27-
<artifactId>spring-boot-starter-web</artifactId>
32+
<artifactId>spring-boot-starter-webmvc</artifactId>
2833
<exclusions>
2934
<!-- Exclude the Tomcat dependency -->
3035
<exclusion>
@@ -39,23 +44,70 @@ The following Maven example shows how to exclude Tomcat and include Jetty for Sp
3944
<artifactId>spring-boot-starter-jetty</artifactId>
4045
</dependency>
4146
----
47+
+
4248
43-
The following Gradle example configures the necessary dependencies and a {url-gradle-docs}/resolution_rules.html#sec:module_replacement[module replacement] to use Tomcat in place of Reactor Netty for Spring WebFlux:
44-
49+
Gradle::
50+
+
4551
[source,gradle]
4652
----
4753
dependencies {
48-
implementation "org.springframework.boot:spring-boot-starter-tomcat"
49-
implementation "org.springframework.boot:spring-boot-starter-webflux"
50-
modules {
51-
module("org.springframework.boot:spring-boot-starter-reactor-netty") {
52-
replacedBy("org.springframework.boot:spring-boot-starter-tomcat", "Use Tomcat instead of Reactor Netty")
53-
}
54+
implementation('org.springframework.boot:spring-boot-starter-webmvc') {
55+
// Exclude the Tomcat dependency
56+
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
5457
}
58+
// Use Jetty instead
59+
implementation "org.springframework.boot:spring-boot-starter-jetty"
5560
}
5661
----
62+
+
63+
======
64+
65+
If you are creating a war file, you can use a similar approach, but you must indicate provided dependencies:
66+
67+
68+
[tabs]
69+
======
70+
71+
Maven::
72+
+
73+
[source,xml]
74+
----
75+
<dependency>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-starter-webmvc</artifactId>
78+
<exclusions>
79+
<!-- Exclude the Tomcat dependency -->
80+
<exclusion>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-starter-tomcat</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
<!-- Use Jetty instead -->
87+
<dependency>
88+
<groupId>org.springframework.boot</groupId>
89+
<artifactId>spring-boot-starter-jetty</artifactId>
90+
<scope>provided</scope>
91+
</dependency>
92+
----
93+
+
5794
58-
NOTE: `spring-boot-starter-reactor-netty` is required to use the javadoc:org.springframework.web.reactive.function.client.WebClient[] class, so you may need to keep a dependency on Netty even when you need to include a different HTTP server.
95+
Gradle::
96+
+
97+
[source,gradle]
98+
----
99+
dependencies {
100+
implementation('org.springframework.boot:spring-boot-starter-webmvc') {
101+
// Exclude the Tomcat dependency
102+
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
103+
}
104+
// Use Jetty instead
105+
implementation "org.springframework.boot:spring-boot-starter-jetty"
106+
providedRuntime "org.springframework.boot:spring-boot-starter-jetty-runtime"
107+
}
108+
----
109+
+
110+
======
59111

60112

61113

module/spring-boot-jetty/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ plugins {
2525
description = "Spring Boot Jetty"
2626

2727
dependencies {
28-
api(project(":module:spring-boot-jetty-runtime"))
2928
api(project(":module:spring-boot-web-server"))
30-
api("jakarta.servlet:jakarta.servlet-api")
31-
api("jakarta.websocket:jakarta.websocket-api")
32-
api("jakarta.websocket:jakarta.websocket-client-api")
29+
api("org.eclipse.jetty.ee11:jetty-ee11-servlets")
30+
api("org.eclipse.jetty.ee11:jetty-ee11-webapp")
31+
32+
implementation("org.eclipse.jetty.compression:jetty-compression-server")
33+
implementation("org.eclipse.jetty.compression:jetty-compression-gzip")
3334

3435
optional(project(":core:spring-boot-autoconfigure"))
3536
optional(project(":module:spring-boot-actuator-autoconfigure"))
3637
optional(project(":module:spring-boot-micrometer-metrics"))
3738
optional("org.apache.tomcat.embed:tomcat-embed-jasper")
3839
optional("org.eclipse.jetty:jetty-alpn-conscrypt-server")
40+
optional("org.eclipse.jetty.ee11.websocket:jetty-ee11-websocket-jakarta-server")
41+
optional("org.eclipse.jetty.ee11.websocket:jetty-ee11-websocket-jetty-server")
3942
optional("org.eclipse.jetty.http2:jetty-http2-server")
4043
optional("org.springframework:spring-webflux")
4144

module/spring-boot-tomcat/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ configurations {
3232

3333
dependencies {
3434
api(project(":module:spring-boot-web-server"))
35-
api(project(":module:spring-boot-tomcat-runtime"))
35+
api("org.apache.tomcat.embed:tomcat-embed-core") {
36+
exclude group: "org.apache.tomcat", module: "tomcat-annotations-api"
37+
}
3638

3739
optional(project(":core:spring-boot-autoconfigure"))
3840
optional(project(":module:spring-boot-actuator-autoconfigure"))
3941
optional(project(":module:spring-boot-micrometer-metrics"))
4042
optional("io.micrometer:micrometer-core")
4143
optional("org.apache.tomcat.embed:tomcat-embed-jasper")
44+
optional("org.apache.tomcat.embed:tomcat-embed-websocket") {
45+
exclude group: "org.apache.tomcat", module: "tomcat-annotations-api"
46+
}
4247
optional("org.springframework:spring-webflux")
4348

4449
runtimeOnly("jakarta.annotation:jakarta.annotation-api")

platform/spring-boot-dependencies/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,6 @@ bom {
20512051
"spring-boot-jdbc-test",
20522052
"spring-boot-jersey",
20532053
"spring-boot-jetty",
2054-
"spring-boot-jetty-runtime",
20552054
"spring-boot-jms",
20562055
"spring-boot-jooq",
20572056
"spring-boot-jooq-test",
@@ -2180,6 +2179,7 @@ bom {
21802179
"spring-boot-starter-jersey",
21812180
"spring-boot-starter-jersey-test",
21822181
"spring-boot-starter-jetty",
2182+
"spring-boot-starter-jetty-runtime",
21832183
"spring-boot-starter-jms",
21842184
"spring-boot-starter-jms-test",
21852185
"spring-boot-starter-jooq",
@@ -2245,6 +2245,7 @@ bom {
22452245
"spring-boot-starter-thymeleaf",
22462246
"spring-boot-starter-thymeleaf-test",
22472247
"spring-boot-starter-tomcat",
2248+
"spring-boot-starter-tomcat-runtime",
22482249
"spring-boot-starter-validation",
22492250
"spring-boot-starter-validation-test",
22502251
"spring-boot-starter-web",
@@ -2266,7 +2267,6 @@ bom {
22662267
"spring-boot-testcontainers",
22672268
"spring-boot-thymeleaf",
22682269
"spring-boot-tomcat",
2269-
"spring-boot-tomcat-runtime",
22702270
"spring-boot-transaction",
22712271
"spring-boot-validation",
22722272
"spring-boot-web-server",

settings.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ include "module:spring-boot-jdbc"
137137
include "module:spring-boot-jdbc-test"
138138
include "module:spring-boot-jersey"
139139
include "module:spring-boot-jetty"
140-
include "module:spring-boot-jetty-runtime"
141140
include "module:spring-boot-jms"
142141
include "module:spring-boot-jooq"
143142
include "module:spring-boot-jooq-test"
@@ -188,7 +187,6 @@ include "module:spring-boot-sql"
188187
include "module:spring-boot-test-classic-modules"
189188
include "module:spring-boot-thymeleaf"
190189
include "module:spring-boot-tomcat"
191-
include "module:spring-boot-tomcat-runtime"
192190
include "module:spring-boot-transaction"
193191
include "module:spring-boot-validation"
194192
include "module:spring-boot-web-server"
@@ -283,6 +281,7 @@ include "starter:spring-boot-starter-jdbc-test"
283281
include "starter:spring-boot-starter-jersey"
284282
include "starter:spring-boot-starter-jersey-test"
285283
include "starter:spring-boot-starter-jetty"
284+
include "starter:spring-boot-starter-jetty-runtime"
286285
include "starter:spring-boot-starter-jms"
287286
include "starter:spring-boot-starter-jms-test"
288287
include "starter:spring-boot-starter-jooq"
@@ -349,6 +348,7 @@ include "starter:spring-boot-starter-test-classic"
349348
include "starter:spring-boot-starter-thymeleaf"
350349
include "starter:spring-boot-starter-thymeleaf-test"
351350
include "starter:spring-boot-starter-tomcat"
351+
include "starter:spring-boot-starter-tomcat-runtime"
352352
include "starter:spring-boot-starter-validation"
353353
include "starter:spring-boot-starter-validation-test"
354354
include "starter:spring-boot-starter-web"

smoke-test/spring-boot-smoke-test-tomcat-jsp/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ configurations {
2929
dependencies {
3030
implementation(project(":starter:spring-boot-starter-webmvc"))
3131

32-
providedRuntime(project(":module:spring-boot-tomcat-runtime"))
32+
providedRuntime(project(":starter:spring-boot-starter-tomcat-runtime"))
3333
providedRuntime("org.glassfish.web:jakarta.servlet.jsp.jstl")
3434
providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
3535

0 commit comments

Comments
 (0)