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

Gradle项目无法找到Shadow插件问题求助

问题:Gradle Shadow插件依赖解析失败导致构建失败

环境信息

$ ./gradlew -version

------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------

Build time:   2022-11-25 13:35:10 UTC
Revision:     daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin:       1.7.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Mac OS X 10.16 x86_64

问题场景

我有一个构建类库的Gradle项目,需要打包生成Fat Jar,按照Shadow插件官方文档配置后,执行./gradlew clean build时出现依赖解析错误,找不到Shadow插件7.1.2版本。

原build.gradle配置

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        gradlePluginPortal()
        maven {
            url = "https://packages.confluent.io/maven"
        }
    }
    dependencies {
        classpath 'gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.1.2'
    }
}

plugins {
    id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
}

apply plugin: 'com.github.johnrengelman.shadow'

repositories {
    jcenter()
    mavenCentral()
    maven {
        url = "https://packages.confluent.io/maven"
    }
    maven {
        url = "https://jitpack.io"
    }
}

dependencies {

    // Kafka
    implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'
    implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173'
    implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14'

    //test
    testImplementation(platform('org.junit:junit-bom:5.9.0'))
    testImplementation('org.junit.jupiter:junit-jupiter')
    testImplementation("io.mockk:mockk:1.9.2")
}

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java"]
        }

        resources {
            srcDirs = ["src/main/avro", "src/main/resources"]
        }
    }
    test {
        java {
            srcDirs = ["src/test/java"]
        }
    }
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

plugins.withId("com.github.johnrengelman.shadow"){

    //this block requires the java plugin to be applied first.
    plugins.withId("java"){

        shadowJar {
            //We are overriding the default jar to be the shadow jar
            classifier = null
            exclude 'META-INF'
            exclude 'META-INF/*.INF'
            exclude 'META-INF/license/*'
        }

        jar {
            manifest {
                attributes(
                        'Built-By'       : System.properties['user.name'],
                        'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
                        'Created-By'     : "Gradle ${gradle.gradleVersion}",
                        'Build-Jdk'      : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                        'Build-OS'       : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
                )
            }
        }

        tasks.build.dependsOn tasks.shadowJar
        tasks.shadowJar.mustRunAfter tasks.jar
    }
}

错误日志

* What went wrong:
A problem occurred configuring root project 'remove-json-value'.
> Could not resolve all files for configuration ':classpath'.
   > Could not find gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.1.2.
     Searched in the following locations:
       - https://jcenter.bintray.com/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
       - https://repo.maven.apache.org/maven2/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
       - https://plugins.gradle.org/m2/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
       - https://packages.confluent.io/maven/gradle/plugin/com/github/jengelman/gradle/plugins/shadow/7.1.2/shadow-7.1.2.pom
     Required by:
         project :

问题原因与解决方法

核心原因

Shadow插件从7.x版本开始,Maven坐标发生了变更,不再使用旧的gradle.plugin.com.github.jengelman.gradle.plugins:shadow坐标;同时Gradle 7+推荐使用Plugins DSL方式引入插件,无需手动在buildscript中配置classpath。另外jcenter仓库已归档停止服务,继续使用可能导致依赖拉取失败。

修改后的build.gradle配置

plugins {
    id 'java'
    // 直接通过Plugins DSL引入Shadow插件,自动处理仓库和依赖
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

repositories {
    mavenCentral() // 替换jcenter为mavenCentral
    maven {
        url = "https://packages.confluent.io/maven"
    }
    maven {
        url = "https://jitpack.io"
    }
}

dependencies {
    // Kafka
    implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'
    implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173'
    implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14'

    //test
    testImplementation(platform('org.junit:junit-bom:5.9.0'))
    testImplementation('org.junit.jupiter:junit-jupiter')
    testImplementation("io.mockk:mockk:1.9.2")
}

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java"]
        }
        resources {
            srcDirs = ["src/main/avro", "src/main/resources"]
        }
    }
    test {
        java {
            srcDirs = ["src/test/java"]
        }
    }
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

// Shadow插件配置无需嵌套,直接配置即可
shadowJar {
    // 覆盖默认jar为shadow jar
    classifier = null
    exclude 'META-INF'
    exclude 'META-INF/*.INF'
    exclude 'META-INF/license/*'
}

jar {
    manifest {
        attributes(
                'Built-By'       : System.properties['user.name'],
                'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
                'Created-By'     : "Gradle ${gradle.gradleVersion}",
                'Build-Jdk'      : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                'Build-OS'       : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
        )
    }
}

tasks.build.dependsOn tasks.shadowJar
tasks.shadowJar.mustRunAfter tasks.jar

关键修改点

  1. 移除buildscript块中的Shadow插件依赖,改用Plugins DSL直接引入插件,Gradle会自动从gradlePluginPortal仓库拉取插件,无需手动配置仓库。
  2. 移除jcenter仓库,替换为mavenCentral,避免归档仓库导致的依赖拉取问题。
  3. 简化插件配置代码,无需嵌套在plugins.withId块中,直接配置shadowJar和jar任务即可,逻辑更清晰。

执行./gradlew clean build即可正常构建生成Fat Jar。

内容的提问来源于stack exchange,提问作者feroze

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 18:51:21