Gradle构建报依赖pom格式Fatal Error但构建成功的成因及解决方法
报错成因
这类打印在日志中的[Fatal Error]本质是本地Gradle依赖缓存内的对应POM元数据文件损坏,和Gradle版本本身无关——这也是你升级到最新版Gradle后报错依旧存在的核心原因。
报错提示的“根元素前的标记格式不合法”,指的是这些POM文件不是合规的XML格式,常见触发场景包括:
- 之前拉取依赖时网络中断,导致POM文件仅下载了半截残缺内容
- 配置的第三方仓库/代理返回了非XML格式的错误页面(比如404 HTML提示),被直接存储为了.pom文件
- 本地安全软件误改写了缓存内的POM文件内容
由于Gradle依赖解析模块内置了容错逻辑,这类损坏的POM不会直接阻断整个构建流程,因此哪怕日志打印了多段Fatal Error,最终仍会输出BUILD SUCCESSFUL。
可落地解决步骤
- 第一步:停掉所有运行中的Gradle后台进程,避免缓存文件被占用无法修改
- 命令行执行
gradle --stop即可关闭所有Gradle Daemon进程
- 命令行执行
- 第二步:定位并删除缓存中已损坏的POM文件
- Gradle本地缓存默认路径:
- macOS/Linux:
~/.gradle/caches/modules-2/files-2.1/ - Windows:
C:\Users\你的用户名\.gradle\caches\modules-2\files-2.1\
- macOS/Linux:
- 对照日志里列出的报错依赖,直接删除对应路径下损坏的POM文件即可,本次需要清理的文件包括:
- commons-text-1.9.pom
- slf4j-log4j12-1.7.30.pom
- junit-4.11.pom
- org.eclipse.jdt.core-3.24.0.pom
- Gradle本地缓存默认路径:
- 第三步:重新触发依赖校验与构建
- 命令行执行
gradle build --refresh-dependencies,强制Gradle重新校验所有依赖的文件完整性 - IntelliJ IDEA端打开右侧Gradle工具面板,点击「刷新所有Gradle项目」按钮,即可触发重新拉取依赖
- 命令行执行
- 兜底处理:如果单个文件清理后仍出现同类POM解析报错,可直接清空整个
modules-2缓存文件夹(注意该操作会触发全量依赖重新下载,首次构建耗时会明显变长)
原始报错素材

Welcome to Gradle 6.8! Here are the highlights of this release: - Faster Kotlin DSL script compilation - Vendor selection for Java toolchains - Convenient execution of tasks in composite builds - Consistent dependency resolution For more details see https://docs.gradle.org/6.8/release-notes.html Starting a Gradle Daemon (subsequent builds will be faster) [Fatal Error] commons-text-1.9.pom:3:3: The markup in the document preceding the root element must be well-formed. [Fatal Error] slf4j-log4j12-1.7.30.pom:3:3: The markup in the document preceding the root element must be well-formed. [Fatal Error] junit-4.11.pom:3:3: The markup in the document preceding the root element must be well-formed. [Fatal Error] org.eclipse.jdt.core-3.24.0.pom:3:3: The markup in the document preceding the root element must be well-formed. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings BUILD SUCCESSFUL in 43s
内容的提问来源于stack exchange,提问作者K. Symbol
相关产品推荐
相关产品推荐

