SLF4J与Jetty依赖冲突求助:类路径同时存在冲突日志JAR包
SLF4J冲突问题求助
添加依赖implementation("org.slf4j:slf4j-log4j12:2.0.7")后,执行代码org.eclipse.jetty.server.Server server = new Server(port);时触发SLF4J冲突错误:
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError
已尝试在所有可能位置排除SLF4J相关依赖,但问题仍复现。我的Gradle配置(build.gradle.kts)如下:
dependencies { implementation("org.eclipse.jetty:jetty-server:$jettyVersion") { exclude(group = "org.slf4j") } implementation("org.eclipse.jetty:jetty-servlet:$jettyVersion") { exclude(group = "org.slf4j") } ... implementation("org.projectlombok:lombok:1.18.24") testAnnotationProcessor("org.projectlombok:lombok:1.18.24") testImplementation("org.projectlombok:lombok:1.18.24") implementation("org.slf4j:slf4j-api:2.0.7") implementation("org.slf4j:slf4j-log4j12:2.0.7") { exclude(group = "log4j") exclude(group = "org.slf4j", module = "log4j-over-slf4j") } configurations.all { resolutionStrategy { eachDependency { exclude(group = "org.slf4j", module = "log4j-over-slf4j") exclude(group = "org.slf4j", module = "slf4j-log4j12") exclude(group = "log4j", module = "log4j") } } }
执行gradlew dependencies --configuration runtimeClasspath得到的依赖树:
+--- org.eclipse.jetty:jetty-server:11.0.8 | +--- org.eclipse.jetty.toolchain:jetty-jakarta-servlet-api:5.0.2 | +--- org.eclipse.jetty:jetty-http:11.0.8 | | +--- org.eclipse.jetty:jetty-util:11.0.8 | | \--- org.eclipse.jetty:jetty-io:11.0.8 | | \--- org.eclipse.jetty:jetty-util:11.0.8 | \--- org.eclipse.jetty:jetty-io:11.0.8 (*) +--- org.eclipse.jetty:jetty-servlet:11.0.8 | \--- org.eclipse.jetty:jetty-security:11.0.8 | \--- org.eclipse.jetty:jetty-server:11.0.8 (*) +--- org.projectlombok:lombok:1.18.24 +--- org.slf4j:slf4j-api:2.0.7 \--- org.slf4j:slf4j-log4j12:2.0.7 \--- org.slf4j:slf4j-reload4j:2.0.7 +--- org.slf4j:slf4j-api:2.0.7 \--- ch.qos.reload4j:reload4j:1.2.22
补充说明:已查阅类似问题,现有方案均要求排除log4j-over-slf4j.jar,但我的依赖树中无此依赖,且已排除多余依赖;确认项目目录中无该JAR,仅本地Maven仓库(.m2)存在。求可行解决办法。
可行排查/解决方向
- 清理缓存与重建:执行
./gradlew clean build --refresh-dependencies清理Gradle缓存并强制刷新依赖;同时清理IDE缓存(如IntelliJ的File -> Invalidate Caches...),避免旧缓存导致的类路径污染。 - 精准排查依赖来源:用命令
./gradlew dependencyInsight --dependency log4j-over-slf4j --configuration runtimeClasspath直接定位log4j-over-slf4j的引入路径,即使依赖树没显示也能找到隐藏的传递依赖。 - 替换绑定依赖:SLF4J 2.x版本中
slf4j-log4j12实际是桥接到reload4j的过渡包,直接改用implementation("org.slf4j:slf4j-reload4j:2.0.7")替代slf4j-log4j12,减少中间桥接环节的冲突可能。 - 检查运行时类路径:确认IDE运行配置中的类路径仅包含项目构建生成的依赖,没有手动添加本地仓库的
log4j-over-slf4j.jar;也可通过java -cp <classpath> -verbose:class启动程序,查看实际加载的类路径,定位冲突jar的来源。
内容的提问来源于stack exchange,提问作者hhrzc
相关产品推荐
相关产品推荐

