简体   繁体   中英

Could not get unknown property "runtime" Gradle 7.0

I switched to gradle 7.0 recently and now cannot build my projects jar, with the error

Could not get unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer. `

Here is my 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
}

Gradle removed the runtime configuration after Gradle 6.x.

You can either change your fatJar task in build.gradle to refer to runtimeConfiguration (as per the Java plugin documentation ):

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
}

Alternatively use a plugin that deals with fat jar building for you. I've tried Shadow a few years ago. This should also take care of files with the same names from different jars (like META-INF/LICENSE ) that you may run into with your approach.

Support for runtime configuration has been stopped gradle-7.0 , so configurations.runtime is not working.

Task copyAllDependencies worked for me in gradle-7.0 .

Here is the example to copy dependencies into ${buildDir}/output/libs :

Add following to build.gradle .

task copyAllDependencies(type: Copy) {
    from configurations.compileClasspath
    into "${buildDir}/output/libs"
}
build.dependsOn(copyAllDependencies)

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM