Ant > Ant Installation
Ant Installation
Ant is free and open source build
tool, written in Java, helps in automating the entire build process of a Java
development project.
- Ant uses XML build files.
- By default, Ant looks for a build file named build.xml.
- The build file contains information about how to build
a particular project.
- Each project contains multiple targets like creating
directory, compiling source codes.
- Target can depend on other targets.
- Targets contain tasks.
- Behind each task is a Java class that performs the
described work.
To install Ant follow the steps
given below.
- Download the latest Ant distribution.
- Extract it (my location is E:\apache-ant-1.7.1)
- Set the following system variables
- ANT_HOME =E:\apache-ant-1.7.1
- PATH = %ANT_HOME%\bin
To make sure the installation is
proper, go to command prompt and execute the command "ant - version",
you will see the installed ant version.
In this example you will see how to
compile a java program and compress it into a .jar file using Ant build file.
The following listing shows the build.xml file.
01.<?xml version="1.0" ?>
02.<project name="Hello
World" default="compress">
03.
04.<target name="compile">
05.<javac srcdir="."/>
06.<echo>
Compilation Complete! </echo>
07.</target>
08.
09.<target name="compress" depends="compile">
10.<jar destfile="HelloWorld.jar" basedir="." includes="*.class" />
11.<echo>
Building .jar file Complete! </echo>
12.</target>
13.
14.</project>
- The <project> element is the root element
in Ant build files. The name attribute of the project element
indicates the project name. Each project element can contain multiple <target>
elements.
- A target represents a single stage in the build process.
A build process can have multiple targets. Here we have two targets compile
and compress.
- The default attribute of project element indicates the
default target to be executed. Here the default target is compress.
- When you see the compress target, it in turn
depends on the compile target, that is indicated by the depends
attribute. So the compile target will be executed first.
- The compile target has two task elements <javac>
and <echo>. The javac task is used to compile the java
files. The attribute srcdir="." indicates all the java
files in the current directory. Echo task is used to display
message on the console.
- The compress target also performs two tasks, first the <jar>
element as the name indicates, is used to build the jar file. The
attributes destfile="HelloWorld.jar" , basedir="."
and includes="*.class" indicates all the .class files in
the current directory should be compressed into HelloWorld.jar
file. Later the echo task is used to display the success message on the
console.
To run the build.xml file,
open the command prompt, go to the example directory, type the command "ant".
You will see the following information.
Sample Ant Build File - JAR
In this example you will see how to
structure the project. If the grows bigger, it will become a problem to manages
the files if all the files are there in the same directory.
For a easier maintenanace all the
source file should be kept in the src directory, the compressed jar file
should be kept in the dist directory and all the intermediate class
files should be kept in the build/classes directory.
By imposing structure cleaning the
project becomes easy, we can just delete the directory and recreate it.
Using the <mkdir> task we
create build/classes and dist directory.
1.<target name="init">
2.<mkdir dir="build/classes" />
3.<mkdir dir="dist" />
4.</target>
During the compilation process 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, creating jar file becomes easier, 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").
1.<target name="compile" depends="init">
2.<javac srcdir="src" destdir="build/classes" />
3.</target>
4.
5.<target name="compress" depends="compile">
6.<jar destfile="dist/HelloWorld.jar" basedir="build/classes" />
7.</target>
You can use the java task to
execute a class file as shown below. The classname attribute refers to
the java class to be executed and the classpath attribute refers to the
directory in which the class is located.
1.<target name="execute" depends="compile">
2.<java classname="com.vaannila.helloworld.HelloWorld" classpath="build/classes" />
3.</target>
Since all the class files and jar
files are isolated, we can easily clean the project by deleting the respective
directories.
1.<target name="clean">
2.<delete dir="build" />
3.<delete dir="dist" />
4.</target>
The default target is compress,
so when you run the build file the compress target will be executed. The
compress target depends on compile target which in turn 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
execute, later the jar file is created.
When you run the "ant
execute" command the execute target will be invoked. Execute
target also depends on compile target which in turn depends on init
target, but now the directories won't be created again because it already
exist. Since the java file is already compiled it won't be compiled again. Ant
checks the timestamp of the file and compiles only the updated files. The HelloWorld.class
file will be execute and the "HelloWorld!" message will be
displayed on the console.
To clean and execute the program run
the "ant clean execute" command. First the clean target
will be executed and the directories will be deleted, later the execute task
will be invoked.
Ant > Ant
Eclipse IDE Integration
Ant Eclipse IDE Integration
To integrate Ant build file with the
Eclipse IDE, first create a build.xml file, right click the project select New->Other->XML,
enter the name as build.xml and click Finish.
01.<?xml version="1.0" ?>
02.<project name="Ant
Example" default="execute">
03.
04.<target name="init" depends="clean">
05.<mkdir dir="build/classes" />
06.</target>
07.
08.<target name="compile" depends="init">
09.<javac srcdir="src" destdir="build/classes" />
10.</target>
11.
12.<target name="execute" depends="compile">
13.<java classname="com.vaannila.helloworld.HelloWorld" classpath="build/classes" />
14.</target>
15.
16.<target name="clean">
17.<delete dir="build" />
18.</target>
19.
20.</project>
To build the project right click the
build.xml file and select Ant Build.
The HelloWorld.java file will
be compiled and executed. The following message shows the sequence of events
that happen once the build file is executed.
01.Buildfile:
E:\Eclipse Workspace\AntExample\build.xml
02.clean:
03.[delete] Deleting
directory E:\Eclipse Workspace\AntExample\build
04.init:
05.[mkdir] Created
dir: E:\Eclipse Workspace\AntExample\build\classes
06.compile:
07.[javac] Compiling 1
source file to E:\Eclipse Workspace\AntExample\build\classes
08.execute:
09.[java] Hello World!
10.BUILD SUCCESSFUL
11.Total time: 625
milliseconds
You can download the build file
here.
Ant > Sample
Ant Build File - WAR
Sample Ant Build File - WAR
I am using the Spring
SimpleFormController example
to illustrate the build process. The figure below shows the structure of the
web application.
All the classes inside the src
directory should be compiled and placed in a separate build/classes
directory. The created war file will be placed inside the dist
directory.
So first we create the build/classes
and the dist directory. The init target does this job.
1.<target name="init">
2.<mkdir dir="build/classes"/>
3.<mkdir dir="dist" />
4.</target>
The next step is to compile all the
classes in the src directory and place them in the build/classes
directory. To do this first you need to add all the lib files inside the "WebContent/WEB-INF/lib"
directory to the classpath.
1.<path id="compile.classpath">
2.<fileset dir="WebContent/WEB-INF/lib">
3.<include name="*.jar"/>
4.</fileset>
5.</path>
The target compile uses the javac
task to compile the java classes and it depends on the target init,
because only when you have the directory ready you can place the classes inside
them.
1.<target name="compile" depends="init" >
2.<javac destdir="build/classes" debug="true" srcdir="src">
3.<classpath refid="compile.classpath"/>
4.</javac>
5.</target>
The path we created earlier will be
refered here using the <classpath> element.
Now we can create the war file using
the war task. To create the war file you need to specify the web
directory, lib directory and the classes directory. The destfile
attribute specifies the war file location and the webxml attribute
specifies the web.xml file location.
1.<target name="war" depends="compile">
2.<war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
3.<fileset dir="WebContent"/>
4.<lib dir="WebContent/WEB-INF/lib"/>
5.<classes dir="build/classes"/>
6.</war>
7.</target>
You can use the clean target to
clean the project. The clean target deletes the build and the dist directory.
1.<target name="clean">
2.<delete dir="dist" />
3.<delete dir="build" />
4.</target>
The complete build.xml file
is shown below.
01.<?xml version="1.0" ?>
02.<project name="AntExample1" default="war">
03.
04.<path id="compile.classpath">
05.<fileset dir="WebContent/WEB-INF/lib">
06.<include name="*.jar"/>
07.</fileset>
08.</path>
09.
10.<target name="init">
11.<mkdir dir="build/classes"/>
12.<mkdir dir="dist" />
13.</target>
14.
15.<target name="compile" depends="init" >
16.<javac destdir="build/classes" debug="true" srcdir="src">
17.<classpath refid="compile.classpath"/>
18.</javac>
19.</target>
20.
21.<target name="war" depends="compile">
22.<war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
23.<fileset dir="WebContent"/>
24.<lib dir="WebContent/WEB-INF/lib"/>
25.<classes dir="build/classes"/>
26.</war>
27.</target>
28.
29.<target name="clean">
30.<delete dir="dist" />
31.<delete dir="build" />
32.</target>
33.
34.</project>
You can download the build file
here.
Ant > Ant
Property Task
Ant Property Task
The <property> task is
used to set the Ant properties. The property value is immutable, once the value
is set you cannot change it. To set a property to a specific value you use
Name/value assignment.
1.<property name="project.name" value="AntExample2" />
To set a property to a location you
use Name/location assignment.
1.<property name="web.dir" location="WebContent"/>
2.<property name="web.lib.dir" location="${web.dir}/WEB-INF/lib"/>
3.<property name="build.classes.dir" location="build/classes"/>
4.<property name="dist.dir" location="dist"/>
To use the properties surround them
with ${}.
The following build file shows how
to set and use property values.
01.<?xml version="1.0" ?>
02.<project name="AntExample2" default="war">
03.
04.<property name="web.dir" location="WebContent"/>
05.<property name="web.lib.dir" location="${web.dir}/WEB-INF/lib"/>
06.<property name="build.classes.dir" location="build/classes"/>
07.<property name="dist.dir" location="dist"/>
08.<property name="project.name" value="AntExample2" />
09.
10.<path id="compile.classpath">
11.<fileset dir="${web.lib.dir}">
12.<include name="*.jar"/>
13.</fileset>
14.</path>
15.
16.<target name="init">
17.<mkdir dir="${build.classes.dir}"/>
18.<mkdir dir="${dist.dir}" />
19.</target>
20.
21.<target name="compile" depends="init" >
22.<javac destdir="${build.classes.dir}" debug="true" srcdir="src">
23.<classpath refid="compile.classpath"/>
24.</javac>
25.</target>
26.
27.<target name="war" depends="compile">
28.<war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
29.<fileset dir="${web.dir}"/>
30.<lib dir="${web.lib.dir}"/>
31.<classes dir="${build.classes.dir}"/>
32.</war>
33.</target>
34.
35.<target name="clean">
36.<delete dir="${dist.dir}" />
37.<delete dir="${build.classes.dir}" />
38.</target>
39.
40.</project>
Ant > Ant
Properties File
Ant Properties File
You can also group all the property
values in a separate properties file and include it in the ant build file. Here
the build.properties file contains all the property values. Remember the
property value is immutable, so if you set a property value in the properties
file you cannot change it in the build file. This give more control over the
build process.
The build.properties file.
1.web.dir=WebContent
2.web.lib.dir=${web.dir}/WEB-INF/lib
3.build.classes.dir=build/classes
4.dist.dir=dist
5.project.name=AntExample3
Use the property task to include the
properties file in the Ant build file.
1.<property file="build.properties" />
Here is the complete build file for
your reference.
01.<?xml version="1.0" ?>
02.<project name="AntExample3" default="war">
03.
04.<property file="build.properties" />
05.
06.<path id="compile.classpath">
07.<fileset dir="${web.lib.dir}">
08.<include name="*.jar"/>
09.</fileset>
10.</path>
11.
12.<target name="init" depends="clean">
13.<mkdir dir="${build.classes.dir}"/>
14.<mkdir dir="${dist.dir}" />
15.</target>
16.
17.<target name="compile" depends="init" >
18.<javac destdir="${build.classes.dir}" debug="true" srcdir="src">
19.<classpath refid="compile.classpath"/>
20.</javac>
21.</target>
22.
23.<target name="war" depends="compile">
24.<war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
25.<fileset dir="${web.dir}"/>
26.<lib dir="${web.lib.dir}"/>
27.<classes dir="${build.classes.dir}"/>
28.</war>
29.</target>
30.
31.<target name="clean">
32.<delete dir="${dist.dir}" />
33.<delete dir="${build.classes.dir}" />
34.</target>
35.
36.</project>
No comments:
Post a Comment