Home > Apache, apache ant, configurating > Building with Apache ANT part 1

Building with Apache ANT part 1

ANT is a java library which can be used to automate the building process of java application. It is a command-line tool. Lot of tasks can be done using ant. Compiling, Testing,Assembling  and as well as running the java applications.

Why we need a build tool

In the process of building applications, there are several things should be done. Rather than doing them manually, a build tool can be used to make this automated. this will really save the developers time and other resources.
Ant is the most complete Java build and deployment tool which is currently available. Ant can be easily invoked from the command line and it can integrate with free and commercial IDEs.
So, lets move on how to build with ANTs.

Installing ANT

Download the latest distribution from here. Then extract it to your own folder (Ex: C:). Make sure that the JAVA_HOME  environmental variable value has been set to the place where your JDK is installed.Then create an another environmental variable called ANT_HOME which is pointed to the Ant installation folder(Ex : C:Apache_ant). Then append the path variable to the location where the  bin folder is located. (Ex:C:Apache_antbin). Now go to command prompt and type  ant -version. Then you get a message which mentioning about the Ant version
C:Apache_ant ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

Starting Building

Ant scripting is basically build on XML. A  xml file, build.xml contains all the commands and details regarding to the build process. So by modifying this xml file we can control whatever the process we need ex: compiling, testing or archiving.
Suppose the project name is myproject. And we need to display a message.First create the project called myproject. Inside the root directory, create build.xml.
<?xml version="1.0"?>
<project name="myproject" default="info">
<target name="info">
<echo>Welcome to Apache Ant</echo>
</target>
</project>

Now go to myproject folder in commant prompt and type ant . This will give the following output.

C:myproject>ant
Buildfile: C:myprojectbuild.xml

info:
[echo] Welcome to Apache Ant

BUILD SUCCESSFUL
Total time: 0 seconds

C:myproject>



The following properties can be used in the build.xml file.

Properties Description
ant.file The full location of the build file.
ant.version The version of the Apache Ant installation.
basedir The basedir of the build, as specified in the basedir attribute of the project element.
ant.java.version The version of the JDK that is used by Ant.
ant.project.name The name of the project, as specified in the name atrribute of the project element
ant.project.default-target The default target of the current project
ant.project.invoked-targets Comma separated list of the targets that were invoked in the current project
ant.core.lib The full location of the ant jar file
ant.home The home directory of Ant installation
ant.library.dir The home directory for Ant library files – typically ANT_HOME/lib folder.

In addition to this the user can define his own properties for the build.xml .

<?xml version="1.0"?>
<project name="myproject" default="info">
<property name="logingname" value="technodyne"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You have logged as
${logingname} </echo>
</target>
</project>  
 C:myproject>ant
Buildfile: C:myprojectbuild.xml

info:
[echo] Apache Ant version is Apache Ant(TM) version 1.8.2
compiled on December 20 2010 - You have logged as technodyne

BUILD SUCCESSFUL
Total time: 0 seconds
C:myproject>
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: