You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. You've modified the default jar task from the java plugin. Normally, this task only packages your project's own compiled classes. But your line from { 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.
  2. The application plugin (which you've applied) in Gradle 6.2 automatically generates an additional -all.jar file when you set mainClassName. This JAR also bundles your dependencies and configures the main class, resulting in two functionally identical JARs in build/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:

  1. Add the Shadow plugin (use a version compatible with Gradle 6.2) to your plugins block.
  2. Keep the application plugin if you need its features, and remove your custom jar task 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:32:29