| Apache Ant is a Java library and command-line tool which is used to build the Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used on non Java applications. In Ant every process is described as task and targets. The Ant consists of few layers for building the applications. The default and the foremost layer is the Project. And the project will have at least one target and the targets will have one or more task. These task elements can be of built-in or it can be user defined also. From this we can easily understand that each task attributed to a individual java class. The below structure will explain basic the structure of the Ant build script |
<project> <target> <task/> <task/> . . . . <task/> </target> . . . . . <target> <task/> <task/> . . . . <task/> </target> </project> |
| And all these elements are put under a single XML file, by default Ant looks for build.xml in your project when you build the application. It’s up to the developer to instruct the Ant to look for a different build script. Mode of use
|
| Ant in Command Line |
| Ant can be invoked from command prompt. Ant provides many built-in command line arguments.We’ll consider few example which we’ll use frequently. |
|
|
|
|
| IDE Integration |
| Netbeans uses Ant as an default build tool to build the applications. Eclipse also provides the option to the user to use Ant as a build tool. When you create an build.xml by default Eclipse gives you the option for running this as Ant Build. Apart from that the “Outline” view provides you the details about individual targets and allows you run that separately. The below image show the Sample build script and it’s integration with Eclipse. |
| Defining Properties |
| Inside the build script file we can define few properties, either it can be internal to build script and we can define a external property file and include that |
<property name="project.name" value="AntDemo" /> <property file="build.properties" /> |
| The first line defines the property called “project.name” with the value of “AntDemo”. The second line includes all the property from the file into build script. |
| Defining Path |
| If we are using any IDE’s for the development of the project then we’ll be referring to common libraries using Build Path. If we wanted to use those libraries during our build operation, we can use PATH attribute. And this can be refereed anywhere in the build script by using tag CLASSPATH. |
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
|
| The below script is the complete build script, which will provide you a complete set of examples to build an application and make an WAR. |
<project name="AntDemo" default="makeWar">
<property name="project.name" value="AntDemo" />
<property file="build.properties" />
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<echo>Deleting the existing Build Directories</echo>
<delete dir="${dist.dir}" />
<delete dir="${classes.dir}" />
</target>
<target name="init" depends="clean">
<echo>Deleting the existing Build Directories</echo>
<mkdir dir="${classes.dir}"/>
<mkdir dir="${dist.dir}" />
</target>
<target name="compile" depends="init" >
<javac destdir="${classes.dir}" debug="true" srcdir="src">
<classpath refid="classpath"/>
</javac>
</target>
<target name="makeWar" depends="compile">
<war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}"/>
<lib dir="${lib.dir}"/>
<classes dir="${classes.dir}"/>
</war>
</target>
</project>
|
| When you run the build script, we’ll get the following output. |
| I’ll add few more task including the custom tasks in the next post. |
Tags: Ant, Build Tools, Core Java












