根据接受的答案,我需要再添加一行代码:
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.yourpackage.Main'
}
archiveClassifier = "all"
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
如果没有这个额外的行,它省略了我的源文件,只添加了依赖项:
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
对于较新的 gradle (7+),您可能会看到以下错误:
Execution failed for task ':fatJar'.
> Entry [some entry here] is a duplicate but no duplicate handling strategy has been set. Please
refer to https://docs.gradle.org/7.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy
for details.
如果发生这种情况,请向任务添加一个类似。duplicatesStrategy
duplicatesStrategy "exclude"
fatJar
同样,对于Gradle 7 +,您只需要删除该行,因为它在此版本的gradle中不再是有效的配置。configuration.compile.collect