You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetty嵌入式应用编译冲突求助:org.eclipse.jetty.server读取jakarta.servlet包重复

解决方案

1. 统一Servlet API依赖为Jetty自带的jetty-jakarta-servlet-api

Jetty 11.0.13配套的jetty-jakarta-servlet-api已经完整包含Jakarta Servlet 5.0的所有API类,且模块名称与官方jakarta.servlet-api一致(均为jakarta.servlet),完全可以替代官方依赖,从根源避免模块冲突。

调整utils模块的pom.xml

将原有官方jakarta.servlet-api依赖替换为Jetty版本,保持provided范围不变:

<dependency>
    <groupId>org.eclipse.jetty.toolchain</groupId>
    <artifactId>jetty-jakarta-servlet-api</artifactId>
    <version>5.0.2</version>
    <scope>provided</scope>
</dependency>

utils模块的module-info.java保留requires jakarta.servlet;声明即可,无需修改。

调整application模块的pom.xml

无需添加任何Servlet API相关的显式依赖——jetty-servlet、jetty-server等Jetty核心组件会自动传递引入jetty-jakarta-servlet-api。确保Jetty组件依赖配置正确:

<dependencies>
    <!-- Jetty核心组件 -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>11.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>11.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-http</artifactId>
        <version>11.0.13</version>
    </dependency>
    <!-- 依赖utils模块 -->
    <dependency>
        <groupId>你的项目groupId</groupId>
        <artifactId>utils</artifactId>
        <version>你的版本号</version>
    </dependency>
</dependencies>

2. 修正application模块的模块声明

application模块的module-info.java只需声明对utils模块及Jetty组件模块的依赖,无需单独添加requires jakarta.servlet;——utils模块已声明该依赖,且Jetty组件模块会间接依赖jakarta.servlet,模块系统会自动处理依赖传递,避免重复声明引发冲突。

示例module-info.java:

module myapplication {
    requires utils;
    requires org.eclipse.jetty.server;
    requires org.eclipse.jetty.servlet;
    requires org.eclipse.jetty.http;
    
    // 若有需要对外暴露的类,添加exports声明
    exports myapplication;
}

3. 清理并重新构建

执行Maven命令清理缓存并重新构建项目:

mvn clean install

核心原理说明

  • 统一依赖Jetty自带的Servlet API,消除了官方jakarta.servlet-api与Jettyjetty-jakarta-servlet-api的同名模块冲突。
  • utils模块的provided范围确保该依赖不会被打包进产物,运行时由嵌入式Jetty提供API实现。
  • application模块通过Jetty组件的传递依赖获取正确的Servlet API,依赖传递机制自动引入jakarta.servlet模块,无需重复声明。

内容的提问来源于stack exchange,提问作者cquezel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:20:34