我的项目有以下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文件看起来构建正常(它只会在它到达依赖于的第一个类时崩溃)番石榴)。任何协助赞赏。 如果它有助于我从IntelliJ做到这一点。
答案 0 :(得分: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) }
}
}