gradlegradle-shadow-plugin

Project unable to find gradle shadow plugin


I have a project that builds a lib. I want to make a fat jar for packaging.

I followed instructions in the gradle-shadow-plugin docs

Here is my 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
    }


}

When I do a .gradlew clean build I am getting the following error.

* 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 :

I search SO and found this answer. However, adding jcenter to the buildscript -> repositories section does not seem to solve it.

This is my environment:

$ ./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

Any ideas what I am doing wrong?


Solution

  • At the time of writing this answer, the shadow plugin's latest version is 2.0.3, not 7.1.2.

    Try the following:

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.3'
    }
    

    The 7.0.x version refers to a different "shadow" dependency, the 2.0.x belongs to the one you specified.