Gradle 6.2项目多Jar生成问题及单可执行Jar构建方法
Great question! Let's walk through why you're getting two JAR files, then show you how to generate a single executable JAR instead.
Why are you seeing the -all.jar file?
Looking at your build.gradle configuration, two key things are causing this duplicate output:
- You've modified the default
jartask from thejavaplugin. Normally, this task only packages your project's own compiled classes. But your linefrom { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }adds all runtime dependencies into this JAR, turning it into a "fat JAR" that includes everything needed to run your app. - The
applicationplugin (which you've applied) in Gradle 6.2 automatically generates an additional-all.jarfile when you setmainClassName. This JAR also bundles your dependencies and configures the main class, resulting in two functionally identical JARs inbuild/libs/.
How to create a single executable JAR
You have two solid options to fix this, depending on your needs:
Option 1: Remove the application plugin (if you don't need its features)
If you don't use the application plugin's tasks like run, distZip, or installDist, just remove it from your plugins block. Keep your custom jar task configuration to generate a single fat JAR:
plugins { id 'com.google.cloud.tools.jib' version '2.2.0' id 'java' } repositories { mavenLocal() mavenCentral() jcenter() } jib.to.image = 'gcr.io/' + project.property('gcpProjectId') + '/my-application' jib.container.creationTime = 'USE_CURRENT_TIMESTAMP' mainClassName = "com.foo.Myapplication" jar { manifest { attributes "Main-Class": "$mainClassName" } from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } // Optional: Handle duplicate files from dependencies to avoid packaging conflicts duplicatesStrategy = DuplicatesStrategy.EXCLUDE }
Run gradle clean build and you'll only get one JAR: my-application-1.0.0.jar, which is fully executable.
Option 2: Use the Shadow Plugin (recommended for fat JARs)
Gradle's official recommended way to build robust fat JARs is using the Shadow plugin—it handles dependency conflicts, duplicate files, and integrates smoothly with both the java and application plugins. Here's how to set it up:
- Add the Shadow plugin (use a version compatible with Gradle 6.2) to your plugins block.
- Keep the
applicationplugin if you need its features, and remove your customjartask configuration (Shadow handles fat JAR creation automatically).
Your final build.gradle will look like this:
plugins { id 'com.google.cloud.tools.jib' version '2.2.0' id 'application' id 'java' id 'com.github.johnrengelman.shadow' version '6.1.0' } repositories { mavenLocal() mavenCentral() jcenter() } jib.to.image = 'gcr.io/' + project.property('gcpProjectId') + '/my-application' jib.container.creationTime = 'USE_CURRENT_TIMESTAMP' mainClassName = "com.foo.Myapplication"
Run gradle clean shadowJar and you'll get a single executable JAR at build/libs/my-application-1.0.0-all.jar. If you want to remove the -all suffix, configure the shadowJar task:
shadowJar { archiveClassifier.set('') // Removes the "-all" suffix from the JAR name }
If you want to keep using gradle clean build to generate the fat JAR, disable the default jar task to avoid duplicates:
jar.enabled = false
内容的提问来源于stack exchange,提问作者SST

