简体   繁体   English

摇篮罐不包含番石榴前提条件

[英]Gradle jar does not include Guava Preconditions

I have the following build.gradle file for my project 我的项目有以下build.gradle文件

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: 但是,当我运行“ gradle jar”并尝试运行给定的jar时,出现错误:

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). 我似乎无法在这里弄清楚我做错了什么,番石榴包含在gradle的依赖项中,否则jar文件似乎可以很好地构建(否则,它只会在到达依赖番石榴的第一类时崩溃)。 Any assistance appreciated. 任何帮助表示赞赏。 If it helps I'm doing this from IntelliJ. 如果有帮助,我可以通过IntelliJ进行。

I solved my problem using the solution at https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571 我使用https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571中的解决方案解决了我的问题

My build.gradle now looks like 我的build.gradle现在看起来像

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) }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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