无法获取未知属性“运行时”Gradle 7.0

Ben*_*ber 10 java gradle

我最近切换到 gradle 7.0,现在无法构建我的项目 jar,并出现错误

无法获取 org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer 类型的配置容器的未知属性“运行时”。`

这是我的 build.gradle:


    apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'



repositories {
    mavenCentral()
}

dependencies {

    implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet-core', version: '2.7'
    implementation group: 'org.eclipse.jetty.aggregate', name: 'jetty-all', version: '9.3.0.M1'
//
    implementation 'javax.xml.bind:jaxb-api:2.3.0'
//    testImplementation group: 'junit', name: 'junit', version: '4.11'
    implementation group: 'org.json', name: 'json', version: '20200518'
    implementation group: 'com.jolbox', name: 'bonecp', version: '0.8.0.RELEASE'
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
    implementation group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '2.12.0'

    implementation "org.slf4j:slf4j-simple:1.7.9"
}

version = '1.0'

jar {
    manifest {
        attributes(
                'Main-Class': 'classes.RestServer',
        )
    }
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/.SF"
        exclude "META-INF/.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

artifacts {
    archives fatJar
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*ert 9

Gradle 6.x 之后删除了运行时配置。

您可以将您的fatJar任务更改build.gradle为参考runtimeConfiguration(根据Java 插件文档):

task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        // change here: runtimeClasspath instead of runtime
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/.SF"
        exclude "META-INF/.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}
Run Code Online (Sandbox Code Playgroud)

或者使用一个为您处理 fat jar 构建的插件。几年前我尝试过影子。这还应该处理META-INF/LICENSE您可能会遇到的来自不同 jar 的同名文件(例如 )。