Create a Fat JAR with Gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 9: Line 9:
=Overview=
=Overview=


A "fat" JAR, or an uber-JAR, is a self-sufficient archive which contains both classes and dependencies needed to run an application. The Gradle Java plugin creates "thin" JARs. However, it can be configured to create a "fat" JAR. Specialized plugins are also available.
A "fat" JAR, or an uber-JAR, is a self-sufficient archive which contains both classes and dependencies needed to run an application. It is also configured with a Main-Class attribute. The Gradle Java plugin creates "thin" JARs. However, it can be configured to create a "fat" JAR. Specialized plugins are also available.


=Creating a Fat JAR with the Jar Task from the Java Plugin=
=Creating a Fat JAR with the Jar Task from the Java Plugin=
The following example reconfigures the default Jar task of the Java plugin to create a fat JAR.
<syntaxhighlight lang='java'>
dependencies {
    ...
    compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
    compile 'com.googlecode.json-simple:json-simple:1.1.1'
    ...
}
jar {
    manifest {
        attributes "Main-Class": "com.example.Application"
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
</syntaxhighlight>
Note that the Jar task configuration uses the "compile" configuration to decide what dependencies to include in the JAR. <font color=darkgray>Come up with an equivalent setup for the 'implementation' configuration.</font>
=Separate Fat Jar Task by Extending the Jar Task of the Java Plugin=
<syntaxhighlight lang='java'>
task customFatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.example.Application'
    }
    baseName = 'uber-jar'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}
</syntaxhighlight>


=Creating a Fat JAR with the Shadow Plugin=
=Creating a Fat JAR with the Shadow Plugin=


<font color=darkgray>Not tested.</font>
<font color=darkgray>TODO</font>

Latest revision as of 00:17, 8 March 2019

External

Internal

Overview

A "fat" JAR, or an uber-JAR, is a self-sufficient archive which contains both classes and dependencies needed to run an application. It is also configured with a Main-Class attribute. The Gradle Java plugin creates "thin" JARs. However, it can be configured to create a "fat" JAR. Specialized plugins are also available.

Creating a Fat JAR with the Jar Task from the Java Plugin

The following example reconfigures the default Jar task of the Java plugin to create a fat JAR.

dependencies {

    ...
    compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
    compile 'com.googlecode.json-simple:json-simple:1.1.1'
    ...
}


jar {

    manifest {

        attributes "Main-Class": "com.example.Application"
    }
 
    from {

        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Note that the Jar task configuration uses the "compile" configuration to decide what dependencies to include in the JAR. Come up with an equivalent setup for the 'implementation' configuration.

Separate Fat Jar Task by Extending the Jar Task of the Java Plugin

task customFatJar(type: Jar) {

    manifest {

        attributes 'Main-Class': 'com.example.Application'
    }

    baseName = 'uber-jar'

    from { 

        configurations.compile.collect { 
            it.isDirectory() ? it : zipTree(it) 
        } 
    }
    with jar
}

Creating a Fat JAR with the Shadow Plugin

TODO