Home > front end >  Building Spark fat jar with Gradle: shadow plugin yields corrupted JAR file
Building Spark fat jar with Gradle: shadow plugin yields corrupted JAR file

Time:02-19

I am trying to build a Spark fat jar with Gradle. The build succeeds but a file comes out corrupted in a subtle way: trying to run it yields:

Error: Could not find or load main class shadow_test.Main
Caused by: java.lang.ClassNotFoundException: shadow_test.Main

The JAR itself looks fine though: the missing class is there and when I unzip it, I can run the project fine.

Here is the gradle.build file:

plugins {
    id "scala"
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

ext {
    ver = [
            scala   : '2.11.12',
            scala_rt: '2.11',
            spark   : '2.4.4'
    ]
}

configurations {
    // Dependencies that will be provided at runtime in the cloud execution
    provided

    compileOnly.extendsFrom(provided)
    testImplementation.extendsFrom provided
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.scala-lang:scala-library:$ver.scala"

    provided "org.apache.xbean:xbean-asm6-shaded:4.10"
    provided "org.apache.spark:spark-sql_$ver.scala_rt:$ver.spark"
    provided "org.apache.spark:spark-hive_$ver.scala_rt:$ver.spark"

    testImplementation "org.testng:testng:6.14.3"
}

tasks.register("allJar", com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
    manifest {
        attributes "Main-Class": "shadow_test.Main"
    }

    from sourceSets.main.output
    configurations = [project.configurations.runtimeClasspath, project.configurations.provided]

    zip64 true

    mergeServiceFiles()

    with jar
}

test {
    useTestNG()
}


Gradle version is 7.3.3

Full code of minimal project that reproduces this problem can be found at https://github.com/SashaOv/shadow-jar-repro

Thanks for any leads

CodePudding user response:

The issue was resolved by Gradle community: https://discuss.gradle.org/t/possible-to-build-spark-fat-jar-with-gradle/42235/2?u=sashao

I was missing exclusions of signature files:

exclude 'META-INF/*.DSA'
exclude 'META-INF/*.SF'
  • Related