build.gradle 无法解析符号 'groovy.json.JsonSlurper'

build.gradle cannot resolve symbol 'groovy.json.JsonSlurper'

我无法使用 Intellij 解析 build.gradle 文件中的 groovy.json.JsonSlurper,有谁知道如何修复它?


plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}



task sample() {
    doLast {
        sample();
    }
}

import groovy.json.JsonSlurper
def sample(){
    def json = new JsonSlurper().parseText('{"a":"b"}')
    println(json);
}

enter image description here

您缺少所需的依赖项。

dependencies 部分添加 org.codehaus.groovy:groovy-json:3.0.9 这样 ti 看起来像

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'org.codehaus.groovy:groovy-json:3.0.9'
}

然后您可以使用 CLI 作为 gradle sample./gradlew sample 进行测试,它将 return {a=b}