Packaging with ANT

Suppose we need to create the following folder structure in our java application.
For this we can use ant.  I create a build.xml file like this.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant-test" default="main" basedir=".">

<property name="src.dir" location="src"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<property name="docs.dir" location="docs"/>

<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${docs.dir}"/>
<delete dir="${dist.dir}"/>

<target name="makedir">
<mkdir dir="${build.dir}"/>
<mkdir dir="${docs.dir}"/>
<mkdir dir="${dist.dir}"/>

<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">

<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**"/>

<target name="jar" depends="compile">
<jar destfile="${dist.dir}technodyne.ant.test.jar" basedir="${build.dir}">
<attribute name="Main-Class" value="test.Main"/>

<target name="main" depends="compile,jar,docs">
<description>Main target</description>

According to the above xml, i created four directories using ANT. Before that , if there are directories with the same name, they are deleted by clean command. Then the java files in thesrc directory are compiled into class files and they are stored in the build directory with the same folder structure as in the src  directoryUsing  javadoc command in ANT, the documents have generated. For that all the files  (include name=”**”) in src folder is being used. Finally, the jar is created according to the content in build directory. We can define our own name for the jar file. Here is the result i got.
Buildfile: D:Eclipse_Indigo_workspacetechnodyne.antbuild.xml
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antbuild
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdocs
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdist
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antbuild
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdocs
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdist
[javac] D:Eclipse_Indigo_workspacetechnodyne.antbuild.xml:24: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to D:Eclipse_Indigo_workspacetechnodyne.antbuild
[jar] Building jar: D:Eclipse_Indigo_workspacetechnodyne.antdisttechnodyne.ant.test.jar
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source file
[javadoc] Loading source file
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.6.0_27
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
Total time: 7 seconds
This is very simple and, once you find the pattern, you can do more and more things with ant. Bye till next post. :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s