Best practice for making a custom distribution with a fat jar

Hi everyone,

I am trying to do something where I’d like to know the proper “gradleonic” (gradley, gradlesque,…?) way to do it before I proceed. Once I know that I suspect I can figure the rest out on the own from the docs.

Background: I am converting a fairly old java desktop project from ant to gradle, and for now am mostly just trying to get everything to work.

What I’d like to do is create a zipped distribution that contains a fat-jar and then a few other directories and files. I can build the fat jar fine. What I don’t know is how best to assemble it with the other things and create the distribution. I think I could either (1) create a new task that just does everything I want by hand, or (2) use the distribution plugin and make modifications to that. I feel like I should do (2) but I’m not sure how to stop a distribution from having a bin / lib layout and execution scripts etc. I just want the fat jar and a few other file and folders.

Here is the build script with the issue:

Any other comments on the style of the script would be most welcome.

I hope this is a legit question relative to the forum guidelines. Thanks in advance,

  • Jeff

Hello,

I feel like I should do (2) but I’m not sure how to stop a distribution from having a bin / lib layout and execution scripts etc

These files come from the application plugin and are generated in the build directory. The distribution plugin only takes files from src/main/dist unless told otherwise, so you should not obtain the files generated by the first plugin in the distribution archive. I think there is a mistake in your distributions DSL:

distributions {
    main {
        contents {
            from uberJar
        }
    }
}

Should create zip or tar archives containing the uberJar and every files under src/main/dist. If that works for you, then you can slowly add new files (from etc/ for example) with filtering, renaming, …

Pay attention to declare the uberJar task before the distributions DSL otherwise gradle will complain about uberJar not being known.

Good luck,

Thanks very much, that was enough to move me forward and I now have achieved what I wanted with your help.

One followup, time permitting, regarding this

Pay attention to declare the uberJar task before the distributions DSL otherwise gradle will complain about uberJar not being known.

Does this mean that the uberJar task must actually be located before the distributions block in the build file? Or do I have to create a dependency of some kind? If there is some topic I should read up on let me know (I’ve read about the task dependencies a bit but that’s it. ).

Why don’t you using the Gradle shadowJar plugin?

Hi,

Does this mean that the uberJar task must actually be located before the distributions block in the build file?

That is exactly what I meant: a mere syntax exception is expected since the uberJar variable is not defined at this point.
The dependency will be managed by Gradle: the from uberJar directive binds all outputs of the uberJar task as input for the CopySpec, and hence draw an implicit dependency between the two.