How to add generated files to shadowJar in build.gradle?

I am using shadowJar in gradle to create 2 jars. One jar contains just main files and the other is a fat jar which contains all java files under src/main/java and src/main/generated. But when I run this jar I get NoClassDefFoundError for classes under src/main/generated. How to achieve this?

My sample shadowJar task is as shown below :

shadowJar {
    dependsOn compileLib
    from(sourceSets.generated.java.srcDirs[1]) 
    from(sourceSets.main.java.srcDirs[1])
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } 
}

task shadowJar2(type: Jar, dependsOn: shadowJar) {
    baseName = project.name + '-all'
    manifest {
        attributes 'Implementation-Title': project.name,
                   'Implementation-Version': version,
                   'Main-Class': 'XYZ'
    }
    doFirst {
        dependsOn compileLib
        from sourceSets.generated.java.srcDirs[1] include '**/*.class'
        from sourceSets.generated.java.srcDirs[0] include '**/*.class'
        from sourceSets.main.java.srcDirs[1]
        from sourceSets.logback.resources.srcDirs[0] include '**/logback.xml'
        from {configurations.applibs.collect { it.isDirectory() ? it : zipTree(it) } }
        from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
    }
}

I tried the suggestions at https://github.com/johnrengelman/shadow/issues/138, https://github.com/bodiam/epub-organizer/blob/master/epub-organizer-dropwizard/epub-organizer-dropwizard.gradle, https://objectpartners.com/2013/07/16/creating-self-contained-executable-jars-with-gradle-and-shadow/ and few others. But I am not able to resolve it. The reason for using shadow jar is that my jar should be properly packaged with all the versions of internal dependent libraries. Any help would be greatly appreciated.

Leave a Reply

avatar
  Subscribe  
Notify of