Home > Apache, apache ant, configurating > Packaging with ANT

Packaging with ANT

Suppose we need to create the following folder structure in our java application.
+---src
+---draw
-Triangle.java
+---test
-TestDraw.java
+---build
+---draw
-Triangle.class
+---test
-TestDraw.class
+---docs
+---dist
-technodyne.ant.test.jar
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>

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

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

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

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

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

</project>
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
clean:
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antbuild
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdocs
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdist
makedir:
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antbuild
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdocs
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdist
compile:
[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:
[jar] Building jar: D:Eclipse_Indigo_workspacetechnodyne.antdisttechnodyne.ant.test.jar
docs:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source file D:Eclipse_Indigo_workspacetechnodyne.antsrcdrawTriangle.java...
[javadoc] Loading source file D:Eclipse_Indigo_workspacetechnodyne.antsrctestTestDraw.java...
[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...
main:
BUILD SUCCESSFUL
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. :)
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: