Gradle项目: Could not find method compile() for arguments

480 阅读1分钟

今天把之前的项目在新的电脑上打开,由于没有现成的环境配置,索性在官网下载最新版本的Gradle开始配置啦。

image.png

环境配置

  • OS: Windows 11
  • JDK: 1.8.0_221
  • Gradle: gradle-8.6-all.zip
  • build.gradle 配置如下:
plugins {  
id 'java'  
id 'com.google.protobuf' version '0.8.13'  
id "com.github.johnrengelman.shadow" version "6.1.0"  
id 'idea'  
}  
  
group 'com.test'  
version '1.2.3'  
sourceCompatibility = 1.8  
targetCompatibility = 1.8  
  
def grpcVersion = '1.62.2'  
def protocVersion = '3.25.3'  
  
repositories {  
mavenCentral()  
}  
  
protobuf {  
protoc {  
artifact = "com.google.protobuf:protoc:$protocVersion"  
}  
plugins {  
grpc {  
artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"  
}  
}  
generateProtoTasks {  
all()*.plugins {  
grpc {}  
}  
}  
}  
  
dependencies {  
implementation "io.grpc:grpc-netty:$grpcVersion"  
implementation "io.grpc:grpc-protobuf:$grpcVersion"  
implementation "io.grpc:grpc-stub:$grpcVersion"  
implementation "io.grpc:grpc-services:$grpcVersion"  
  
implementation 'com.alibaba:druid:1.2.9'  
  
implementation 'org.slf4j:slf4j-simple:1.7.25'  
implementation 'org.zeroturnaround:zt-process-killer:1.10'  
  
implementation 'javax.annotation:javax.annotation-api:1.3.2'  
  
compile "commons-cli:commons-cli:1.4"  
  
testCompile group: 'junit', name: 'junit', version: '4.12'  
}  
  
// if you have source imports issues, add the below  
sourceSets.main.java.srcDir new File(buildDir, 'generated/source')  
idea {  
module {  
// Marks the already(!) added srcDir as "generated"  
generatedSourceDirs += file('build/generated/source')  
}  
}  
  
jar {  
manifest.attributes 'Main-Class': 'com.test.MdExtractorProxy'  
}

问题现象

  • 执行build命令,输入如下报错提示:
A problem occurred evaluating root project 'extractor-proxy'.
> Could not find method compile() for arguments [commons-cli:commons-cli:1.4] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
  • 问题分析: 参考 StackOverflow 上的解决方案
    • Note that the compileruntimetestCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10 (Aug 27, 2018), and were finally removed in Gradle 7.0 (Apr 9, 2021).
    • The aforementioned configurations should be replaced by implementationruntimeOnlytestImplementation, and testRuntimeOnly, respectively.
    • 原始项目中 Gradle 使用的版本是 6.7.1,而现在使用的版本是8.6;

修改配置文件为如下:

image.png 编译成功:

image.png