Building and Publishing an Uber jar in a Jenkins-Gradle-Artifactory setup

Introduction

Assuming you have a Jenkins-Gradle-Artifactory system set up correctly and you in fact have a Java project being compiled, built and published to Artiafactory correctly, there is a chance you would like to build an Uber jar, in the case of one of our customers this in Uber jar was required to include all the Build ouputs (in this case in was two jars: product.jar & product-api.jar) and all the dependent jars

Building the Uber jar

To build the uber jar all you need is an additional task like so:

task uberJar(type: Jar, group:'Extra Jars', description: 'Assembles a single \'uber\' jar archive containing the main classes along with all its dependencies.') {
    classifier = 'uber'    
    from files(sourceSets.main.output.classesDir)
    from(configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }) {
        //exclude all META-INF that may appear in any dependant jar
        exclude "META-INF/**"
    }
}

The sourceSets.main.output.classesDir  will bring in all the Build output jars , and the configurations.runtime.collect  will bring in all the dependecies 

Making sure the Uber jar is published to Artifactory

While the Artifactory-Jenkins plugin Configuration basic setup, specfically the Publish artifacts to Artifactory section enables Publishing of the default jars packaged, other jars (e.g. with classifiers, like javadocs and in our case uber jar) are ignored.

To ensure the product-uber.jar is published do the following:

  1. Add the following to the main section in build.gradle:

        configurations {
            published
        }
        artifactory {
            publish {
                defaults {
                    publishConfigs('published')
                }
            }
        }

        artifacts {
            published jar
        }

  2. And the following (in bold) to the Uber task:
    task uberJar(type: Jar, group:'Extra Jars', description: 'Assembles a single \'uber\' jar archive containing the main classes along with all its dependencies.') {
        classifier = 'uber'
        from files(sourceSets.main.output.classesDir)
        from(configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }) {
            //exclude all META-INF that may appear in any dependant jar
            exclude "META-INF/**"
        }
        artifacts {
            published uberJar
        }

    }

  3. The Artifcatory-Gradle script auto-created during the Build of the Jenkins job will take care of Publishing to Artifactory

Senior DevOps Engineer

DevOps Group