Gradle : How to create a Jar file

How to name a Jar file

Lets take the following jar file for explaning the jar naming conventions.

customName_wrapper_2.0.1_SNAPSHOT.jar

A jar file has the following properties

Property Description
baseName customName
appendix wrapper
version 2.0.1
classifier SNAPSHOT
archiveName -

Here property archiveName if specified will override all the other naming conventions like baseName etc. It should be a fully qualified name of the Jar including the extension (.jar)

Other Properties of Gradle Task :: Jar

Property Description
destinationDir Destination where jar is to be created
manifest Include Manifest file
from Directory from where files are to be read

Creating a Jar file

Let's assume the following directory structure

Project Structure

Create a Jar file with all files.

We can write a gradle task to include all files in src to our jar file as follows

task createAll(type : Jar){
    from 'src'
}

When we run the gradle task as gradle -q createAll will give the following files in the jar

META-INF/
META-INF/MANIFEST.MF
main/
main/java/
main/java/com/
main/java/com/ekiras/
main/java/com/ekiras/demo/
main/java/com/ekiras/demo/D.java
main/java/com/ekiras/demo/A.java
main/java/com/ekiras/demo/E.java
main/java/com/ekiras/demo/C.java
main/java/com/ekiras/demo/F.java
main/java/com/ekiras/demo/B.java
main/java/com/ekiras/demo/p1/
main/java/com/ekiras/demo/p1/X.java
main/java/com/ekiras/demo/p1/Z.java
main/java/com/ekiras/demo/p1/Y.java
test/
test/java/
main/resources/
test/resources/

Create a Jar file from a Package.

task createFromPackage(type : Jar){
    from ("src/main/java/com/ekiras/demo/p1")
}

Run the above task with command gradle -q createFromPackage, it will give the following files in jar.

META-INF/
META-INF/MANIFEST.MF
X.java
Z.java
Y.java

Create a Jar file with Selected Files.

task createSelectedJar(type : Jar){
    from ('src/main/java/com/ekiras/demo'){
        includes = ["A.java","B.java", "p1/X.java", "p1/Y.java"]
    }
}

Run the above command with command gradle -q createSelectedJar

META-INF/
META-INF/MANIFEST.MF
A.java
B.java
p1/
p1/X.java
p1/Y.java

Create a Jar file excluding some files

task createExcludingFiles(type :Jar){
    from ('src'){
        excludes = ["main/java/com/ekiras/demo/D.java","main/java/com/ekiras/demo/E.java", "main/java/com/ekiras/demo/p1/**"]
    }
}

Run the gradle task with command gradle -q createExcludingFiles. It will result in a jar file with the following files

META-INF/
META-INF/MANIFEST.MF
main/
main/java/
main/java/com/
main/java/com/ekiras/
main/java/com/ekiras/demo/
main/java/com/ekiras/demo/A.java
main/java/com/ekiras/demo/C.java
main/java/com/ekiras/demo/F.java
main/java/com/ekiras/demo/B.java
test/
test/java/
main/resources/
test/resources/

To Sum up all the exmaples above

Following is a gradle task that create a jar file

task createJar(type: Jar){
    from ('src/main/java/com/ekiras/demo'){
        includes = ["A.java","B.java", "p1/**"]
        excludes = ["D.java","E.java"]
    }
    baseName = 'customName'
    appendix = 'wrapper'
    version = "2.0.1"
    classifier = 'SNAPSHOT'
    archiveName = "${baseName}_${appendix}_${version}_${classifier}.jar"
    extension  = "ext"
    destinationDir = new File("${buildDir}/test/a")

    manifest {
        attributes = [
                'Main-Class' : "com.ekiras.A",
                'Version'    : 2.0
         ]
    }
}

we can also write a gradle task to see the files that were created inside the jar file as follows

task showFiles(dependsOn : createJar){
    doLast{
        FileTree files = zipTree("${buildDir}/test/a/${archiveName}")
        files.each {
            println "${it.name}"
        }
    }
}

When we run the task gradle -q createJar showFiles or simply gradle -q showFiles , it will give the following result

A.java
B.java
MANIFEST.MF
X.java
Y.java
Z.java

No comments:

Powered by Blogger.