简体   繁体   中英

Gradle jar does not include Guava Preconditions

I have the following build.gradle file for my project

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.guava:guava:23.6-jre';
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

jar {
    manifest {
        attributes 'Main-Class': 'Runner.ClientRunner'
    }
}

However, when I run "gradle jar" and attempt to run the given jar, I get the error:

java.lang.NoClassDefFoundError: com/google/common/base/Preconditions

I can't seem to nail down what I've done wrong here, guava is included in the dependencies for gradle and the jar file appears to build fine otherwise (it only crashes when it gets to the first class that depends on guava). Any assistance appreciated. If it helps I'm doing this from IntelliJ.

I solved my problem using the solution at https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571

My build.gradle now looks like

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    // configuration that holds jars to include in the jar
    extraLibs
}

dependencies {
    compile 'com.google.guava:guava:23.6-jre';
    extraLibs group: 'com.google.guava', name: 'guava', version: '23.6-jre'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    configurations.compile.extendsFrom(configurations.extraLibs)
}

jar {
    manifest {
        attributes 'Main-Class': 'Runner.ClientRunner'
    }
    from {
        configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

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