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/

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

How to start Tomcat on more than one Port.

Tomcat settings can be found in TomcatFolder/conf/server.xml file.

  1. Root element of the configuration is Server tag.
  2. You can add multiple Service under Server tag for running tomcat on more than one port.

Default Service Configuration should look as follows

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>

The above configurations states the folllowing things

  1. Service name="Catalina" - Service name is Catalina, you can change it to give your application name.
  2. Connector port="8080" protocol="HTTP/1.1" - Tomcat will run on port 8080
  3. connectionTimeout="20000" - Connection timeout in 20000 mili seconds
  4. Connector port="8009" protocol="AJP/1.3" - AJP connection on port 8009.
  5. name="localhost" - Name of the host where tomcat will run.
  6. appBase="webapp" - Base directory for deployed apps is webapp
  7. unpackWARs="true" - The war files deplyed wll be unpacked

Running Tomcat on multiple ports.

To start tomcat on more than one port you can add multiple Service blocks under Server root tag as shown below

  <Service name="app1">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="app1"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="app1_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
  <Service name="app2">
    <Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8091" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="app2"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="app2_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>

Here in above configuration we have specified the following.

  1. Service app1 configuration will run on port 8080.
  2. Service app2 configuration will run on port 8090.
  3. app1 will keep files and war files in folder app1.
  4. app2 will keep files and war files in folder app2.
  5. app1 and app2 will write access logs in folder logs/app1_access_log.txt and logs/app2_access_log.txt respectively

Note : None of the above configuration will keep files or war files in webapp folder.

Also Read

How to change default port 8080 of Tomcat

Look for the file server.xml in folder path-to-Tomcat/conf/

This file containes the configuration for tomcat. Look for the following lines in this file.

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Connector port="8080" defines the port on which the tomcat will run. If you want to run tomcat on any port other than 8080 then you can just change the port number here.

e.g Running Tomcat on port 8090

<Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

e.g Running Tomcat on port 9000

<Connector port="9000" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Also Read

How to change Working Directory of Tomcat to Custom Directory

Default Tomcat working directory is webapp. If you want to change this to some other directory then you can follow the steps below.

Open the server.xml file. You can find this file in conf/ directory of your tomcat folder.

Find the following line in the file


     <Host name="localhost"  appBase="webapps"
           unpackWARs="true" autoDeploy="true">

It states the following things

  1. name="localhost" - name of the server
  2. appBase="webapps" - working directory of tomcat where applications are deployed
  3. unpackWARs="true" - if tomcat should unpack war files.
  4. autoDeploy="true" - if tomcat should auto deploy changes

How to change Work directory of tomcat.

To change the work directory from webapp to some other directory you can give the

  1. absolute path to the directory where you want to deploy apps.
  2. relative path to the directory inside the Tomcat folder.

e.g Deploying in directory with absolute path


<Host name="localhost"  appBase="/home/ekiras/tomcat/deploy"
           unpackWARs="true" autoDeploy="true">

Deploying in directory with relative path


<Host name="localhost"  appBase="custom"
         unpackWARs="true" autoDeploy="true">

How to load and read files in Gradle Project.

The Gradle Project Api has the following methods for using files.

  1. file() - to load and read a particular file
  2. files() - to load and read multiple files
  3. fileTree() - to load and read file hierarchy in a directory or folder
  4. zipTree() - to load and read a zip file (includes Jar , War and Ear files)
  5. tarTree() - to load and read a tar file

How to list files inside a Zip file Jar File and Tar file

Lets say we have a file structure as shown in image below.

File Structure

Following Gradle task will list the files inside a zip file, jar file and a tar file at the location "src/main/resources/archives/"

task showFiles(){
    doLast{
        FileTree zipFiles = zipTree("src/main/resources/archives/resources.zip");
        FileTree jarFiles = zipTree("src/main/resources/archives/resources.jar");
        FileTree tarFiles = tarTree("src/main/resources/archives/resources.tar.gz");

        println " \n#Contents of Zip File"
        zipFiles.each {
            println "${it.name}"
        }

        println " \n#Contents of Jar File"
        jarFiles.each {
            println "${it.name}"
        }

        println " \n#Contents of Tar File"
        tarFiles.each {
            println "${it.name}"
        }
    }
}

When we run the above gradle task using command gradle -q showFiles, it will give the following output.

 
#Contents of Zip File
file1.properties
sample1.txt
sample2txt
 
#Contents of Jar File
file1.properties
sample1.txt
sample2txt
 
#Contents of Tar File
sample1.txt
file1.properties
sample2txt

Read More

Difference Between File, FileCollection and FileTree in Gradle

File FileCollection FileTree
File is from java.io package FileCollection is from org.gradle.api.file package FileTree is from org.gradle.api.file package
File represents a single file. FileCollection represents a collection of files FileTree represents hierarchy of files in a directory or folder.

Difference Between File, FileCollection and FileTree

Suppose we have a directory structure as shown in image below.

File Structure

We can write a gradle task as shown below


task loadFiles(){
    doLast{

        File file = file('src/main/resources/sample1.txt')
        println file.name
        println "\n"

        FileCollection collection = files('src/main/resources/sample1.txt','src/main/resources/sample2.txt')
        collection.each {
            println it.name
        }
        println "\n"

        FileTree tree = fileTree('src')
        tree.each {
            println it.name
        }
    }
}

When we run the above gradle task using command gradle -q loadFiles, it will give the following output.

sample1.txt


sample1.txt
sample2.txt


sample1.txt
sample2txt
file1.properties
A.java

Read More