Gradle无法生成Fat Jar但Maven可正常构建问题求助
问题背景
尝试通过Gradle构建Fat Jar时,无论是使用Kotlin脚本还是Groovy脚本(参考过两种主流方案),均出现「无法解压指定POM文件」的错误,但使用Maven插件可正常生成Fat Jar。
相关脚本
Gradle Groovy脚本
plugins { id 'java' } repositories { mavenCentral() } dependencies { testImplementation(platform("org.junit:junit-bom:5.10.0")) testImplementation("org.junit.jupiter:junit-jupiter") implementation("org.neo4j.driver:neo4j-java-driver:5.27.0") implementation("org.mongodb:mongodb-driver-sync:4.8.2") implementation("org.mongodb:mongodb-driver:3.12.7") implementation( "co.elastic.clients:elasticsearch-java:8.12.0") implementation( "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.17.16") implementation( "org.elasticsearch.client:transport:7.17.17") implementation( "com.datastax.oss:java-driver-core:4.15.0") implementation( "commons-logging:commons-logging:1.1.3") implementation( "org.apache.hive:hive-jdbc:3.1.2") implementation( "com.fasterxml.jackson.core:jackson-annotations:2.18.1") implementation( "com.fasterxml.jackson.core:jackson-core:2.18.1") } jar { manifest { attributes "Main-Class": "com.baeldung.fatjar.Application" } from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } } task customFatJar(type: Jar) { manifest { attributes 'Main-Class': 'com.baeldung.fatjar.Application' } archiveBaseName = 'all-in-one-jar' duplicatesStrategy = DuplicatesStrategy.EXCLUDE from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } with jar }
Gradle Kotlin脚本
plugins { id("java") } group = "org.example" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { testImplementation(platform("org.junit:junit-bom:5.10.0")) testImplementation("org.junit.jupiter:junit-jupiter") implementation("org.neo4j.driver:neo4j-java-driver:5.27.0") implementation("org.mongodb:mongodb-driver-sync:4.8.2") implementation("org.mongodb:mongodb-driver:3.12.7") implementation( "co.elastic.clients:elasticsearch-java:8.12.0") implementation( "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.17.16") implementation( "org.elasticsearch.client:transport:7.17.17") implementation( "com.datastax.oss:java-driver-core:4.15.0") implementation( "commons-logging:commons-logging:1.1.3") implementation( "org.apache.hive:hive-jdbc:3.1.2") implementation( "com.fasterxml.jackson.core:jackson-annotations:2.18.1") implementation( "com.fasterxml.jackson.core:jackson-core:2.18.1") } tasks.jar { manifest.attributes["Main-Class"] = "com.example.MyMainClass" val dependencies = configurations .runtimeClasspath .get() .map { zipTree(it)} from(dependencies) duplicatesStrategy = DuplicatesStrategy.EXCLUDE } tasks.test { useJUnitPlatform() }
错误信息
Execution failed for task ':customFatJar'.
Cannot expand ZIP '/Users/user1/.gradle/caches/modules-2/files-2.1/org.apache.curator/apache-curator/2.12.0/b1ff6ce0741facb15217ae9254ee7167bc7b15e/apache-curator-2.12.0.pom'.
- Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org. BUILD FAILED in 31s 2 actionable tasks: 1 executed, 1 up-to-date
可行的Maven方案
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>org.example</mainClass></manifest> </archive> </configuration> </plugin> </plugins> </build>
解决方法
1. 清理Gradle缓存
执行以下命令,强制刷新依赖并清理可能损坏的缓存文件:
./gradlew clean build --refresh-dependencies
2. 修改脚本过滤非Jar文件
错误核心是runtimeClasspath中混入了POM文件,导致Gradle尝试解压非Zip格式的文件。修改脚本仅处理Jar文件:
Groovy脚本修改
将customFatJar和jar任务中的from块替换为:
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : (it.name.endsWith('.jar') ? zipTree(it) : null) }.findAll() }
Kotlin脚本修改
将tasks.jar中的依赖处理部分替换为:
val dependencies = configurations .runtimeClasspath .get() .map { if (it.name.endsWith(".jar")) zipTree(it) else null } .filterNotNull() from(dependencies)
3. 使用Shadow插件(推荐)
Gradle官方更推荐使用Shadow插件构建Fat Jar,它会自动处理依赖过滤、重复类等问题:
Groovy脚本配置
plugins { id 'java' id 'com.github.johnrengelman.shadow' version '8.1.1' } shadowJar { manifest { attributes 'Main-Class': 'com.baeldung.fatjar.Application' } duplicatesStrategy = DuplicatesStrategy.EXCLUDE }
Kotlin脚本配置
plugins { java id("com.github.johnrengelman.shadow") version "8.1.1" } tasks.shadowJar { manifest.attributes["Main-Class"] = "com.example.MyMainClass" duplicatesStrategy = DuplicatesStrategy.EXCLUDE }
执行构建命令:
./gradlew shadowJar
生成的Fat Jar位于build/libs/[项目名]-[版本]-all.jar。
内容的提问来源于stack exchange,提问作者echo

