Gradle : How To Exclude Files and Packages from a Jar file

How To Exclude File from a Jar file.

Gradle task Jar has a property excludes which takes an array as an input and exclude these files from the Jar file it creates.

Let's assume the following directory structure

Project Structure

Following example excludes files from the Jar file.

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

This will exclude files D.java, E.java from the jar file. Run the gradle task using command gradle -q createExcludingFiles. The Jar file created will have 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
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/

Exclude Package in Jar file

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

Run the task using command gradle -q createExcludingPackage. This will remove

  1. All the files from package p1
  2. Class D.java

The Jar file created will have 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/E.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/

No comments :

Post a Comment