Create your own ant build file to create a jar file.


Apache Ant is a build tool mainly focus on Java to build Java applications. Ant uses the xml script file to perform tasks such as,
  • compiling source code, 
  • running software tests, 
  • creating jar files, 
  • creating Javadocs, and 
  • keeping your own file structure within the project folder.
 As I earlier explained Ant uses simple xml file to perform its tasks. Therefore let's see how to write a simple xml file for Ant to compile and create a jar file using Java source classes.
I have to mention here there are 2 kind of jar files,
  1. jar file which can be use as a library to another program.
  2. jar file which can be run (runnable) on the operating system.
Ant can use to either create jar file or runnable jar file. First let's create a jar file(not a runnable jar file).

Structure of the xml file.
Ant xml file includes mainly  three elements,
  1. Property - can be consider as a variable of the xml file.
  2. Target - can be consider as a function in C which can be reuse and modularized.
  3. Task - can be consider as a command on the terminal or C.
Initially let's start with a simple task.

First create a file named 'build.xml' and type following codes.


<project default="final" name="yourProjectName">

</project>

Code explanation........
name in project tag-
Name of your project should be here
default in project tag-
Default target script will start to run(Explained later)
Now create a simple target(function) to execute task(command). As a first step let's echo some text to terminal.


<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>
</project>

Code explanation........
name in target tag-
Name of this target(function) this name can be use to recall the target
echo-
Use to print text on terminal
Now run the xml file
  • In terminal,
    • change the directory to directory which include the build.xml file(using 'cd' command) and type ant and press enter.
  • In Eclipse,
    • Right click on source file, select Run as -> Ant Build file
It will print Hello world... on the terminal/console screen.

Now let's consider about two targets (functions).

<project default="final" name="yourProjectName">
           <target name="initial" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>

          <target name="final" depends="initial" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>

</project>


Code explanation........
depends in target tag-
This is very important attribute to make our program flow correctly. Here first, program will direct to default target which is defined in project tag. Now program wait until the 'initial' target finish its tasks at 'final' target. ('final' target depends on 'initial' target therefore after finishing 'initial' target's tasks, the target 'final' can be started). Finally program flow will be firstly 'initial' target and then 'final' target.
Now let's try to crate jar file using Ant

Create jar file using ant

In order to create jar file there should be some requirements,
  • Basically, Java source code file(s).
  • compile versions of these Java source files.
  • well understood file structure within the project folder.

Therefore first organize our project folder by creating folders and placing our files into correct locations.

src folder use to place Java source files.
lib folder use to place all necessary external jar files.
build folder use to place temporary class files(compiled files)
dist folder use to place final jar file which is created by Ant


After organizing the project folder, place your build.xml file in the directory which all the above folders included.

Initially, let's compile our Java source files to create class files. To accomplish that the task(command) 'javac' can be used. javac is a task which is defined in Ant(more details can be found at Ant reference manual)

syntax-
<javac srcdir="sourceDir" destdir="destinationDir"/>
srcdir-
full path to the directory which include Java source file(s)
destdir-
full path to the directory, compiled file(s) to be created(destination to create .class files)
Now full code to compile Java source file(s) looks like follow,

<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to compile Java source files">
                    <javac srcdir="sourceDir" destdir="destinationDir"/>
           </target>
</project>


 Code explanation........
Here, javac task simply embed into a target in order to make program more clear. This code can be expect some exception, generating destination directory not found or something like that. To prevent that exception we should create a destination folder(because destination folder does not exist in the project folder)

mkdir task can be used to create a new directory.

<project default="compileMe" name="yourProjectName">
           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="destinationDir"/>
                    <javac srcdir="sourceDir" destdir="destinationDir"/>
           </target>
</project>


Code explanation........
mkdir-
make a new directory, name defined by dir attribute

As you can see here, still we did not use any real path for codes. Because the path is depend on user's disk names, disk letter, user names, etc.
The best way to give a path is give it as a relative path to the task. Now property element take place to create relational variable based on paths(locations on the disk). So if we declare an relative variable who is the referrer? OR the variable relative to what?
Therefore we have to give base directory to the program to refer that location and create relative variables for location.

Declare properties and compile Java source file(s)

<project default="compileMe" name="yourProjectName" basedir=".">
    
            <property name="srcDir" location="src"/>
            <property name="buildDir" location="build"/>

           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="${buildDir}"/>
                    <javac srcdir="${srcDir}" destdir="${buildDir}"/>
           </target>
</project>


Code explanation........
basedir in project tag-
base directory of all the location property(Here it is '.' that is the directory  itself which save the build.xml file)
name in property tag-
Name of the property(can give any meaningful name)
location in property tag-
Relative location of the disk. As an example, let's say my basedir is '/home/milinda/workspace/AntTest' when Ant assign value to srcDir it concatenate base directory and relative property's location to make absolute path of the directory. Finally srcDir = '/home/milinda/workspace/AntTest/src'
${buildDir}-
This is the way to access to a property in Ant build file(here ${buildDir}=/home/milinda/workspace/AntTest/build and ${srcDir}=/home/milinda/workspace/AntTest/src)

Now this Ant file will run correctly on any directory with the src (java source file directory) directory.


Create jar file using Ant

jar is the task which is defined by Ant to create jar file by using Java class files and other configuration files.

Syntax-
<jar jarfile="destinationDir/jarfileName.jar" basedir="baseOnWhatDir">

destinationDir/jarfileName.jar-
full path of jar file to be created.
basedir-
full path to the directory which compiled file(s) are located.
Now we can create another directory for save the jar file which can be created by using compiled Java files from 'build' directory. Finally code for making jar file is looks like.


<project default="makeJar" name="yourProjectName" basedir=".">
    
            <property name="srcDir" location="src"/>
            <property name="buildDir" location="build"/>
            <property name="distDir" location="dist"/>

           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="${buildDir}"/>
                     <javac srcdir="${srcDir}" destdir="${buildDir}"/>
           </target>

           <target name="makeJar" depends="compileMe" description="this is a function to make jar file">
                      <mkdir dir="${distDir}"/>
                      <jar jarfile="${distDir}/finalRelease.jar" basedir="${buildDir}"/>
            </target>
</project>





6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. gud work milinda..sadly i saw ur blog after exams ne..but it is nice to see dat ur witing things very essential to us..

    i hope u wont mind if i add something more ,

    machnng adding "manifest" tag inside the "jar" tag will help u to mention about main class inside build.xml and let you run jar file using terminal without mentioning main class at the end of java command

    ReplyDelete