spring-bootgradledependency-managementjetty-10

Switch Spring Boot 2.7 to Jetty 10 with Gradle


Due to a NullPointerException recently introduced in Jetty v9.4.51, I wanted to take a look on how to switch to Jetty v10 which is also supported by Spring Boot v2.7. With Jetty v9 having reached its EOL almost 1 yr ago, it's also about time even though Jetty v9 is still and will remain the default when using spring-boot-starter-jetty.

The docs of Spring Boot do actually contain a section on how to switch to Jetty v10, but I lack the fantasy on how this is supposed to work and I haven't managed yet to get the dependency resolution to change to v10.

I'm aware of the general concept to centralize version properties in a Maven POM and reference the properties in the actual dependency declaration like

<properties>
    <jetty.version>10.0.14</jetty.version>
</properties>

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>${jetty.version}</version>
</dependency>

I have no clue though which magic makes Maven or Gradle resolve a transitive dependency differently with the property set but without any explicit reference to jetty.version in the example given in Spring Boot docs:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <!-- exclusions removed for sake of brevity -->
</dependency>

Or do I actually have to explicitly declare the dependency on Jetty v10 independent from spring-boot-starter-jetty (which I don't understand from the docs)? Then again, I wouldn't just exclude Jetty v9-specific dependencies related to websocket.

I also tried following solution using ext given in a similar case:

ext['jetty.version'] = '10.0.14'

dependencies {
  implementation ('org.springframework.boot:spring-boot-starter-jetty:2.7.10') {
    exclude group: 'org.eclipse.jetty.websocket', module: 'websocket-server'
    exclude group: 'org.eclipse.jetty.websocket', module: 'javax-websocket-server-impl'
  }
}

The Jetty dependencies org.eclipse.jetty:jetty-servlets and org.eclipse.jetty:jetty-webapp keep getting resolved to v9.4.51.v20230217. So which point am I missing? Is there a way to easily switch Spring Boot v2.7 to Jetty v10 without declaring dependency on Jetty v10 explicitly? Does that maybe only work when using Spring's BOM (which we are not)?


Solution

  • Does that maybe only work when using Spring's BOM (which we are not)?

    Yes. Using jetty.version to override the version of Jetty relies upon either using spring-boot-starter-parent with Maven or using the dependency management plugin with Gradle.

    If you are not using Spring Boot's dependency management, you will have to override Jetty's version using a standard mechanism provided by Maven or Gradle. For example, with Maven you could import Jetty's bom into your project's dependency management in its pom.xml:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-bom</artifactId>
                <version>10.0.14</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>