Gradle 7.5构建项目报错:找不到'compile'配置求助
Gradle 7.5升级后报错“Configuration with name 'compile' not found”的解决
问题描述
将原本采用Gradle 6或更低版本构建的项目升级至Gradle 7.5后,已将所有依赖声明从compile/testCompile替换为api/testImplementation,但执行gradle build时仍报错:Configuration with name 'compile' not found。
已查阅资料
已查阅以下StackOverflow讨论:
- Build error with gradle Could not find method testCompile()
- What's the difference between implementation, api and compile in Gradle?
项目根Gradle配置
buildscript { repositories { mavenCentral() } dependencies { // https://github.com/google/protobuf-gradle-plugin classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10' } } plugins { id 'application' id 'java' id 'java-library' id "com.google.protobuf" version "0.8.10" } group 'com.ght' version '61' mainClassName = 'com.application' sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { // jcenter() is no longer support. // jcenter() mavenCentral() } sourceSets { main { proto { srcDir 'src/main/protobuf' include '**/*.protodevel' } } } dependencies { testImplementation group: 'junit', name: 'junit', version: '4.12' implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0' // https://mvnrepository.com/artifact/ch.qos.logback/logback-classic implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.3' // https://mvnrepository.com/artifact/ch.qos.logback/logback-core implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.1.3' // https://mvnrepository.com/artifact/org.slf4j/slf4j-implementation implementation group: 'org.slf4j', name: 'slf4j-implementation', version: '1.7.26' // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.2.1' // https://mvnrepository.com/artifact/org.locationtech.jts/jts-core implementation group: 'org.locationtech.jts', name: 'jts-core', version: '1.16.0' // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.4' // https://mvnrepository.com/artifact/com.vividsolutions/jts implementation group: 'com.vividsolutions', name: 'jts', version: '1.13' // https://mvnrepository.com/artifact/com.google.guava/guava implementation group: 'com.google.guava', name: 'guava', version: '19.0' // https://mvnrepository.com/artifact/org.apache.commons/commons-math3 implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.5' // https://mvnrepository.com/artifact/commons-logging/commons-logging implementation group: 'commons-logging', name: 'commons-logging', version: '1.2' } protobuf { // Configure the protoc executable protoc { // Download from repositories artifact = 'com.google.protobuf:protoc:3.0.0' } generatedFilesBaseDir = "$projectDir/gen" } clean { delete protobuf.generatedFilesBaseDir } jar{ from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } manifest { attributes('Manifest-Version': archiveVersion, 'Main-Class': 'com.application') } exclude 'logback-test.xml' exclude '**/schema/**' exclude '**.proto' }
构建错误信息
A problem occurred configuring root project 'application'. > Configuration with name 'compile' not found. * Try: > Run with --info or --debug option to get more log output. * Exception is: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'application'. at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:84) at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:77) at org.gradle.configuration.project.LifecycleProjectEvaluator.access$400(LifecycleProjectEvaluator.java:55) at org.gradle.configuration.project.LifecycleProjectEvaluator$NotifyAfterEvaluate.run(LifecycleProjectEvaluator.java:255) . . . Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'compile' not found. at org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.createNotFoundException(DefaultConfigurationContainer.java:108) at org.gradle.api.internal.DefaultNamedDomainObjectCollection.getByName(DefaultNamedDomainObjectCollection.java:333) at org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.getByName(DefaultConfigurationContainer.java:98) at org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.getByName(DefaultConfigurationContainer.java:50) . . . at com.google.protobuf.gradle.ProtobufPlugin$_setupExtractIncludeProtosTask_closure21.doCall(ProtobufPlugin.groovy:369) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.gradle.util.internal.ClosureBackedAction.execute(ClosureBackedAction.java:73) at org.gradle.util.internal.ConfigureUtil.configureTarget(ConfigureUtil.java:155) at org.gradle.util.internal.ConfigureUtil.configureSelf(ConfigureUtil.java:131) at org.gradle.api.internal.AbstractTask.configure(AbstractTask.java:666) at org.gradle.api.DefaultTask.configure(DefaultTask.java:309) . . .
环境信息
- Gradle版本:7.5
- Gradle JVM版本:GraalVM 17.0.3
- IDE:IntelliJ IDEA 2022.1.3
解决思路与方案
问题根源
错误栈显示问题来自com.google.protobuf.gradle.ProtobufPlugin的setupExtractIncludeProtosTask方法,旧版本的protobuf-gradle-plugin(0.8.10)仍在引用Gradle 7.x中已被移除的compile配置,导致配置查找失败。
解决方案
升级protobuf-gradle-plugin版本
将插件版本升级至至少0.8.17及以上(推荐使用兼容Gradle 7.x的稳定版本,如0.8.18),新版本插件已适配Gradle 7的配置模型,会使用implementation/api替代旧的compile配置。清理重复的插件配置
移除buildscript块中关于protobuf插件的声明,保留plugins块的声明即可,避免重复加载导致的冲突。
修改后的核心配置示例
// 移除原有的buildscript块 plugins { id 'application' id 'java' id 'java-library' id "com.google.protobuf" version "0.8.18" // 升级插件版本 } // 其余配置保持不变...
- 验证修复
执行gradle clean build命令,验证构建是否正常完成。
内容的提问来源于stack exchange,提问作者Joe Chen
相关产品推荐
相关产品推荐

