Gradle构建报错:无法找到org.codehaus.groovy:groovy:4.0.12依赖
检查仓库配置
确认项目build.gradle(或build.gradle.kts)的repositories块中包含Maven Central这类能获取Groovy依赖的仓库,避免重建项目时仓库配置丢失或遗漏:repositories { mavenCentral() }若使用私有仓库,需确认仓库内存在Groovy 4.0.12的对应包,或配置了公网仓库的代理。
验证依赖范围与版本兼容性
由于仅在Spock测试中使用Groovy,需确保Groovy相关依赖(包括Spock本身)都声明在testImplementation或testRuntimeOnly范围内,避免依赖传递异常。同时要保证Spock版本与Groovy版本匹配,比如Spock 2.3-groovy-4.0对应Groovy 4.0.x系列,示例配置:dependencies { testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0' testImplementation 'org.codehaus.groovy:groovy-ant:4.0.12' }清理Gradle缓存
本地缓存损坏或未完整下载的依赖可能导致解析失败,执行以下命令强制刷新依赖并清理缓存:./gradlew clean build --refresh-dependencies也可手动删除Gradle缓存目录(Linux/macOS为
~/.gradle/caches,Windows为C:\Users\<用户名>\.gradle\caches)后重新构建。排查网络与代理问题
确认当前网络可访问Maven Central的Groovy 4.0.12资源页面。若使用代理,检查gradle.properties中的代理配置参数(如systemProp.http.proxyHost)是否正确,避免重建项目时代理配置丢失。检查Gradle版本兼容性
Groovy 4.0.12要求Gradle 7.0及以上版本支持,查看gradle/wrapper/gradle-wrapper.properties中的distributionUrl,确认使用的Gradle版本符合要求:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip检查依赖排除规则
其他依赖可能通过exclude规则移除了Groovy模块,执行以下命令生成测试类路径的依赖树,查找是否有Groovy依赖被意外排除:./gradlew dependencies --configuration testRuntimeClasspath
内容的提问来源于stack exchange,提问作者Thom

