Gradle jar不包括番石榴先决条件

问题描述 投票:0回答:1

我有我的项目的以下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'
    }
}

但是,当我运行“gradle jar”并尝试运行给定的jar时,我收到错误:

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

我似乎无法确定我在这里做错了什么,guava包含在gradle的依赖项中,而jar文件看起来构建正常(它只会在它到达依赖于guava的第一个类时崩溃)。任何协助赞赏。如果它有助于我从IntelliJ做这个。

java intellij-idea gradle guava
1个回答
0
投票

我使用https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571的解决方案解决了我的问题

我的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) }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.