Mike Thomsen
Mike Thomsen

Reputation: 37526

Gradle munges directory names while creating a jar with all dependencies

This is my task:

task uberJar(dependsOn: [classes], type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from { 
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes 'Main-Class': 'cli'
    }
}

It's a very simple project, one that just builds a single groovy script into a self-contained, executable jar file. I know the Groovy code works. When I tried to run java -jar on the generated jar file, it said it could not find a file that I knew was included. So I expanded the jar file and found that several packages had been munged. This is the folder structure I found:

enter image description here

Any idea why it is doing this? This task is based on a common one that I find all over the place for how to build a jar with all of the dependencies using Gradle.

Upvotes: 0

Views: 97

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123986

The Jar contents look correct. Most if not all of the top-level directories depicted come from groovy-all, which apparently is a dependency of your code. A JVM won't load classes from nested Jars (change-password.jar) unless a custom class loader is used.

Using class names that don't start with an upper-case letter (cli) can lead to problems in Groovy. More importantly, merging Jars isn't generally safe and can lead to subtle problems. A better way to create a self-contained executable Jar is to use the gradle-one-jar plugin.

PS: Instead of the first two from lines, from sourceSets.main.output should be used. Then the explicit dependsOn (which by the way is missing processResources, which would explain missing resource files) can also be removed. If you don't need the regular Jar, you can reconfigure the jar task rather than adding an uberJar task. (If you go for this, omit from sourceSets.main.output.)

Upvotes: 1

Related Questions