Gradle构建的jar没有要运行的依赖项
这不是我第一次遇到这个问题。我正在尝试使用Gradle组装一个.jar文件,其中包含maven依赖项,这样我就可以使用如下命令运行程序: $java-mx900m tregexWrapper-1.0.jar NP taskstence.txt 然而,考虑到我的jar文件大小为3kb,我感觉Gradle不太可能包含我想要的依赖项 这是结果:线程主java.lang.NoClassDefFoundError中出现异常:edu/stanford/nlp/trees/tregex/TregexPattern 这是我的build.gradle文件,我甚至明确地将依赖项添加到了运行时,但没有帮助Gradle构建的jar没有要运行的依赖项,gradle,Gradle,这不是我第一次遇到这个问题。我正在尝试使用Gradle组装一个.jar文件,其中包含maven依赖项,这样我就可以使用如下命令运行程序: $java-mx900m tregexWrapper-1.0.jar NP taskstence.txt 然而,考虑到我的jar文件大小为3kb,我感觉Gradle不太可能包含我想要的依赖项 这是结果:线程主java.lang.NoClassDefFoundError中出现异常:edu/stanford/nlp/trees/tregex/TregexPatte
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = "Entry"
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Main-Class': 'Entry'
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
compile group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.3.1'
runtime(
[group: 'commons-cli', name: 'commons-cli', version: '1.2'],
[group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.3.1']
)
}
run {
if (project.hasProperty('args')) {
args project.args.split('\\s+')
}
}
问题的原因是什么?我使用以下源集获得了良好的结果:
jar {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
完整示例:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
compile group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.3.1'
runtime(
[group: 'commons-cli', name: 'commons-cli', version: '1.2'],
[group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.3.1']
)
}
sourceCompatibility = 1.7
mainClassName = "hello.Main"
//task superjar(type: Jar) {
jar {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
jar.doFirst {
manifest {
attributes 'Implementation-Title': 'Falkonry Gram',
'Implementation-Version': version,
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
'Main-Class': mainClassName
}
}
我认为您的run命令在tregexWrapper-1.0之前缺少-jar。jar@navicore是的,谢谢你指出这一点。jar{后面的前两行是多余的,可能会导致重复的文件。configurations.compile.collect应该是configurations.runtime.collect。你也可以使用gradle fatjar插件:@Opal我想我会使用fatjar