ANT

Building Java Application with Ant and Eclipse Example

Apache ANT (Another Neat Tool) is an open-source & highly flexible Build Tool used for automated compiling, updating, testing & deploying Java Applications.One major advantage of ANT is that, it does not enforce any directory layout or other such coding conventions.

In this example, we shall show how to use Apache Ant (1.9) with Eclipse IDE, to build a Java Application. So without further ado, lets start!

 

 

Project Environment:

1. Apache Ant (1.9)  download here

2. Eclipse 4.5(Mars)

3. JDK (1.7.0_67)

 ANT Installation:

Step 1: Download ANT binary distribution from above link.

Step 2: On windows right click on my Computer>Advanced System Settings>Environment variables>System variables>ADD

In the name put  “ANT_HOME” & for variable value copy-paste the path in which ANT binary is extracted.

Figure 1: ANT Installation
Figure 1: ANT Installation

Now open the path variable & update it with ANT_HOME variable as shown in picture below:

Figure 2: Editing Path variable during ANT Installation
Figure 2: Editing Path variable during ANT Installation

 

For Linux/Unix Systems, we use following :

export ANT_HOME=/usr/local/ant
export PATH=${PATH}:${ANT_HOME}/bin

On successful installation, executing ant -version command shows output like :

Figure 3: successful ANT installation CMD screen
Figure 3: successful ANT installation CMD screen

Beginning with Eclipse :

Eclipse provides us with built-in view for ANT. We can open the view by clicking Window>Show view>ANT

Figure 4: ANT View in Eclipse
Figure 4: ANT View in Eclipse

 

Now that we are all set lets get going with some actual coding.

Build File :

The Build Fileis an XML file, which contains information about how to build a project.

build.xml

 <?xml version="1.0" encoding="UTF-8"? >
 <project name="JCG" basedir="." default="builddist" >

	 <property file="build.properties" > </property >

	 <!-- The Class-Path for the build is defined here-- >
	 <path id="cp" >
		 <fileset dir="${lib.dir}" includes="*.jar" / >
	 </path >
	
	 <!-- The Class-Path for the JUnit Test is defined here.This also includes the compiled classes directory-- >
	 <path id="jUnit.cp" >
		 <fileset dir="${lib.dir}" includes="*.jar" / >
		 <pathelement location="${bin.dir}" / >
	 </path >

	 <!-- Clean the bin, dist & report folder  -- >
	 <target name="clean" >
		 <delete dir="${bin.dir}" / >
		 <delete dir="${dist.dir}" / >
		 <delete dir="${test.reports.dir}" / >
	 </target >

	 <!-- Create the bin,dist & report folders for fresh build -- >
	 <target name="init" depends="clean" >
		 <mkdir dir="${bin.dir}" / >
		 <mkdir dir="${dist.dir}" / >
		 <mkdir dir="${test.reports.dir}" / >
	 </target >

	 <!-- Compilation of Java Src Files into bin folder -- >
	 <target name="compile" depends="init" >
		 <echo >Compiling now... </echo >
		 <javac destdir="bin" debug="true" srcdir="${src.dir}" includeantruntime="false" >
			 <classpath refid="cp" / >
		 </javac >
		 <echo >Compilation successful! </echo >
	 </target >
	
	 <!-- Package the build into a JAR File after compilation & testing tasks are completed-- >
	 <target name="builddist" depends="compile,test" >
		 <jar destfile="${dist.dir}/jcg.jar" basedir="${bin.dir}" excludes="**/*Test.class" >
		 </jar >
	 </target >
	
	
	 <!-- 	This task is Used to Unit-Test the Compiled Java Files -- >
	 <target name="test" >
		 <junit printsummary="yes" fork="true" haltonfailure="yes" >
			 <classpath refid="jUnit.cp" / >
			 <formatter type="plain" / >
			 <batchtest fork="yes" todir="${test.reports.dir}" >
				 <fileset dir="${src.dir}" >
					 <include name="**/*Test.java" / >
				 </fileset >
			 </batchtest >
		 </junit >
	 </target >

 </project >

The project is the root element in the build.xml. The name attribute in the project tag is used to specify the name of the project.There can be multiple elements in a single project element.There is also a default attribute to the project element, which indicates the default task for the build file. The basedir ="." is used to mark the directory in which build.xml file is present as the working directory. The basedir ="." is used to mark the directory in which build.xml file is present, as the working directory. We will discuss about build.properties in a while.

Figutre 5 : Project Layout
Figutre 5 : Project Layout

The targets may depend on other targets, like jar depends on compilation which itself, depends on init & so on. The build can be initiated by dragging the build.xml file into the ANT View as shown in pic and double clicking on it.

Figure 6 : Sample ANT Build File View in Eclipse Mars
Figure 6 : Sample ANT Build File View in Eclipse Mars

Figure 7 : Sample ANT output in Eclipse
Figure 7 : Sample ANT output in Eclipse

Individual tasks can be run in isolation by double clicking only on the required task. The compilation & some other targets may require availability of certain JAR files into the class-path. The class-path is set using the path tag & specifying the related JAR & class files. It can be latter referred from the required targets using the refid attribute in the classpath tag.

Build Property File :

build.properties

src.dir = src
lib.dir = ${basedir}/lib
bin.dir = ${basedir}/bin
dist.dir = ${basedir}/dist
test.reports.dir = ${basedir}/testreports

The properties are usually declared at the top of the build.xml file. However, if the project is big, declaring build properties in a separate file is advisable. The properties file can be included in the build.xml using the file attribute in the property tag. ANT properties files, follow the java.util.Properties convention.

Automated Unit Testing :

The ANT can be used to automatically, Unit test the Java Files. The junit tag is used for this task. It requires the following JAR files:

  1. ant-junit.jar
  2. hamcrest-core-1.3.jar
  3. junit-4.11.jar

The generated reports can be saved to a directory in the required format(plain txt, XML etc.) as shown in the above example. The user may choose to halt or proceed with the build ,in case, the JUnit test cases fail using the haltonfailure attribute. The fork attribute is used to run the test cases in a separate VM. Care should be taken while defining the class-path to include the compiled class-files. The result of the test is directed to the report directory mentioned in the todir attribute of bachtest tag.

Additional Notes :

We may create our ANT targets by extending the Task class from ANT and using the taskdef tag.

Conclusion

Here we tried to automate our Application building process using some basic targets of Apache ANT. There are many more targets offered by ANT for our ease. We can extend the same technology to build J2EE  & other none-Java applications like C,C++ as well.

Download
You can download the full source code of this example here :Eclispe-ANT BUILD Example

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button