Introduction To ANT Build Tool - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Introduction To ANT Build Tool

Share This


If you are developing an application in Java, at the time of development, you have to compile the code build the archive file and put it in the desired location. Even if you have made a few changes in source code, like fixing a bug, you have to go through very time-consuming phases.


Thus, to reduce this time, the developers prefer to use a build tool. Mostly used build tools are Ant and Maven. In this tutorial, we will discuss, how to use Apache Ant tools.

Apache Ant is a very simple, flexible, and powerful XML-based Java project build tool. The configuration file (build.xml) of Ant is simpler than Apache Maven, which makes this tool very popular. Let us start with a case study.


Case Study: Web Application Deployment

The build.xml Ant script shown below builds a war file for the 'euca_client' project.

The name of the project is specified as an attribute of the project tag. In this configuration file, references are expressed using the ${...} expression. Property is essentially a variable, and we have several properties defined at the top of the file.

In this configuration file, the classpath has been set to include additional jar files, add property files and add an SQL script.

Then a target clean is set to delete the content of the build directory of the project.

In this file, another target compile is set to compile the source code and keep the files in the desired location.

The next target createwar is written to create a war file to deploy the web application on the server. If you look at this target carefully, you will see that this target depends on the target compile. So, if you call createwar, automatically target compile will be called before executing this.

The targets deploy and undeploy are created to deploy the war on the server and undeploy the war file from the server respectively.

Now, look at the flow of the phases. If you call deploy, it will execute createwar which in turn will execute compile. Thus you don't need to execute the phases individually, just you can run deploy and all the associated tasks will be executed in the background.

<project name="euca_client" default="compile" basedir="."> <!-- Name of project and version --> <property name="proj.name" value="Euca_Client" /> <property name="proj.shortname" value="euca_client" /> <property name="deploy.dir" value="/root/jboss-4.2.2.GA/server/default/deploy" /> <property name="version" value="1.0" /> <!-- Global properties for this build --> <property name="app.dir" value="." /> <property name="src.dir" value="src" /> <property name="classes.dir" value="classes" /> <property name="lib.dir" value="lib" /> <property name="build.dir" value="build" /> <property name="config.dir" value="conf" /> <property name="image.dir" value="images" /> <!-- Classpath declaration --> <path id="project.classpath"> <path path="${runtime-classpath}" /> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> <fileset dir="${config.dir}"> <include name="**/*.properties" /> <include name="**/*.sql" /> </fileset> </path> <!-- Clean up --> <target name="clean" description="Clean the build directory"> <delete dir="${build.dir}" /> </target> <!-- Compile Java source --> <target name="compile"> <mkdir dir="${build.dir}" /> <javac srcdir="${src.dir}/com/" destdir="${classes.dir}" debug="true" classpathref="project.classpath" /> </target> <target name="createwar" depends="compile"> <war destfile="${build.dir}/euca_client.war" webxml="${app.dir}/web.xml"> <fileset dir="${app.dir}/jsps" /> <fileset dir="${config.dir}" /> <fileset dir="${app.dir}/images" /> <lib dir="${lib.dir}" excludes="servlet-api.jar" /> <classes dir="${classes.dir}" /> </war> </target> <target name="create-tables"> <sql driver="com.mysql.jdbc.Driver" userid="root" password="" url="jdbc:mysql://localhost:3306/mysql" src="${config.dir}/createDB.sql"> <classpath path="${lib.dir}/mysql-connector-java-5.1.5-bin.jar" /> </sql> </target> <target name="deploy" depends="createwar"> <copy file="${build.dir}/euca_client.war" todir="${deploy.dir}" /> </target> <target name="undeploy"> <delete file="${deploy.dir}/euca_client.war"></delete> </target> </project>
Let's examine the targets and understand how they work. There is a 'depends' attribute that specifies other targets that should be run before running this target.

The mkdir task makes the directory if it doesn't already exist.

The delete task deletes the file if it already exists.

The warfile attribute of the createwar task specifies the path and file name of the war file to create.

The web.xml attribute specifies where our web.xml to be used is located.

The classes tag specifies the location of our project's Java classes to be placed in the war file.


Happy Exploring


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.