How To Build Jar Files Using Ant
In this tutorial you will see how to create jar files using ant. Before discussion we need to know some key concepts which is mentioned below.
In real time we need to deal with large no of files and it will become a problem to manages the files if all files resides in the same directory after building the project.
For easier maintenance of project all the source file should be kept in the src directory,all the compiled .class files should be kept in the build/classes directory and all the generated jar file should be kept in the dist directory.
Creating the build file
Rather than putting all the tasks together in one long list of actions, we’ve broken the separate stages—directory creation,compilation, packaging,and clean-up into four separate targets inside the build file.build.xml is given below.
In this tutorial you will see how to create jar files using ant. Before discussion we need to know some key concepts which is mentioned below.
In real time we need to deal with large no of files and it will become a problem to manages the files if all files resides in the same directory after building the project.
For easier maintenance of project all the source file should be kept in the src directory,all the compiled .class files should be kept in the build/classes directory and all the generated jar file should be kept in the dist directory.
Creating the build file
Rather than putting all the tasks together in one long list of actions, we’ve broken the separate stages—directory creation,compilation, packaging,and clean-up into four separate targets inside the build file.build.xml is given below.
build.xml
<?xml version="1.0" ?> <project name="HelloWorld" default="compress"> <target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist"/> </target> <target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="compress" depends="compile"> <jar destfile="dist/HelloWorld.jar" basedir="build/classes" /> </target> <target name="clean"> <delete dir="build" /> <delete dir="dist" /> </target> </project>
Let us split the build file line by line so it will become more easy to learn.
This build file adds an init target to do initialization work, which means creating directories.
Using the <mkdir> task we create build/classes and dist directory.
<target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist"/> </target>
During the compilation process(compile target) all the java files in the src directory will be compiled and the generated class files will be placed in the build/classes directory.
Since we placed all the class files under the build/classes directory, so for creating jar file , you can simply specify the basedir attribute as "build/classes" instead of specifying basedir="." and includes="*.class".
After creating the jar file we place it in the dist directory (destfile="dist/HelloWorld.jar").Refer the script below for more clarification.
<target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="compress" depends="compile"> <jar destfile="dist/HelloWorld.jar" basedir="build/classes" /> </target>
The clean target cleans up the output directories by deleting them. It uses a new task, <delete>.Script for deleting directory is mentioned below.
<target name="clean"> <delete dir="build" /> <delete dir="dist" /> </target>
The default target is compress, so when you run the build file the compress target will be executed.
Refer following line of build file
<target name="compress" depends="compile"> and <target name="compile" depends="init">
The compress target depends on compile target and compile target depends on the init target.so first the init target will be executed and the two directories will be created, then the compile target will be executed, and after that jar file is created.
Simple Project to build jar file
Step 1: Creating the project directory
Create a directory and name it to antjarexample in any of your local drive.Everything will go inside this directory like: source files, created output files, and the build file.Inside this directory create subdirectory src -> com -> jwt -> ant
Step 2 : Create Java Class inside this directory
Now create a java class Hello.java and add following code into this file.
Hello.javapackage com.jwt.ant; public class Hello { public static void main(String args[]) { System.out.println("Hello World! Welcome to javawebtutor.coma"); } }
Step 3 : Write build file(build.xml)
Write the same build file which we already discussed above inside this directory.
build.xml<?xml version="1.0" ?> <project name="HelloJar" default="compress"> <target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist"/> </target> <target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="compress" depends="compile"> <jar destfile="dist/Hello.jar" basedir="build/classes" /> </target> <target name="clean"> <delete dir="build" /> <delete dir="dist" /> </target> </project>
Step 4 : Running the build
Open the command prompt and navigate to the folder where the build.xml resides, and type ant.If the build file has been typed correctly, then you should see the following output on cmd:
Now open the directory you can see the generated build and dist directory. You can find generated jar file inside dist directory.
Related Articles