Ant simple intro

This article introduces apache ant

  1. Ant Introduction
    1. Simple project template
    2. Simple tasks – Properties, paths and initialization
      1. Properties
      2. Paths
      3. Init – create / delete dirs
    3. Common tasks
      1. Javac – java compile
      2. Jar – java archive
      3. XSL – Xml Transformation
    4. Other tasks
      1. path & fileset
      2. copy
      3. ftp
    5. Advanced Tasks
  2. References

Ant Introduction

ANT is a tool for automating software build processes, implemented in Java, and best suited to build Java projects. Ant uses an XML file to describe the build process and its dependencies.

Ant is an Apache project, it is open source software, released under the Apache Software License.

Simple project template

Here follows a simple buildfile that

  • initializes properties and classpath
  • compile java classes
  • build jar and javadoc
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="build" >
    <description>simple project buildfile</description>
    <target name="init"
        	description="initialize properties and classpath">
        <property name="dir.sources" value="src"/>
        <property name="dir.lib" value="lib"/>
        <property name="" value="build"/>
        <property name="" value="${}/classes"/>
        <path id="classpath.project">
          <fileset dir="${dir.lib}" includes="*.jar"/>
    <target name="build" depends="compile"
        	description="builds the project">
        <jar jarfile="${}/jar/${}.jar">
          <fileset dir="${}" includes="**" />
    	<javadoc destdir="${}/doc">
    		<fileset dir="${dir.sources}" defaultexcludes="yes" includes="**/*.java"/>
    <target name="compile" depends="init"
        	description="compile java classes">
      <javac srcdir="${dir.sources}" destdir="${}">
        <classpath refid="classpath.project"/>

below we present a small list with common ant task.

Simple tasks – Properties, paths and initialization

A small list of common and widely used ant tasks


Usually you work with a list of properties that defines file, folders and project variables. below two examples of property definition:

<!-- simple property definition -->
<property name="packages-prefix" value="test-archive"/>
<!-- properties loaded by property file -->
<property file=""/>

The second example import all properties from the externam file


Another important issue is to define classpath to use inside your projects. The example shows the definition of a classpath

<path id="">
	<fileset dir="${dir.lib}/*.jar" />

In large projects, may be that you have to define multiple classpaths: one for compile, one for test, etc.

Init – create / delete dirs

Initialization means create or delete directories. An example, frequently used, is the creation of a particular directory structure, ot the deletion of directories containing generated artifacts. The code below shows example of creatin an deleting files/folders.

<delete dir="${}" includeemptydirs="true" includes="**/*"/>
<mkdir dir="${}/html" />
<mkdir dir="${}/fo"/>
<mkdir dir="${}/pdf"/> 

Common tasks

Here I lisk most common tasks

Javac – java compile

Javac, compile java classes from srcdir to destdir, using the specified classpath. You could also define exclusion filter, compilation level and other parameters. Below there is an example of more complete call to <javac/> task

<javac srcdir="${dir.sources}" destdir="${}" 
		excludes="**/test/**" debug="true" 
		target="1.6" source="1.6" encoding="UTF-8" includeantruntime="false">
    <classpath refid="project.class.path" />
    <compilerarg value="-Xlint:unchecked" />

Jar – java archive

the Jar task is used to build a *.jar* archive from compiled source and other resources. Below there is an invocation of ant task, with

<jar jarfile="${}/jar/${name.archive}" 
		defaultexcludes="yes" update="true">
	<fileset dir="${dir.resources}/jar/includes" />
	<fileset dir="${}/"
		excludes="**/test/**" />
	<manifest file="${dir.resources}/jar/" />

XSL – Xml Transformation

The xsl task performs an XML transformation on provided documents. Below there is an example that passes input parameters to the transformation

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="style/apache.xsl">
  <param name="date" expression="07-01-2000"/>

If you need to use more complex features, like specifyng a catalog, use factory settings, or other; please read the ant manual > xslt task

Other tasks

path & fileset

<path id=""> 
	<fileset dir="${dir.lib}/compile">
		<include name="*.jar"/>
		<exclude name=".jar" />
	<fileset dir="${dir.lib}/test">
		<include name="*.jar"/>
		<!--<exclude name="${jboss-libs}"/>-->
	<!--<pathelement location="${jboss-common-lib}/antlr.jar"/>-->
	<!--<fileset dir="D:\workspace\\lib\test" includes="*.jar"/>-->
	<pathelement location="${}"/>


copy task example: Copy a set of files to a local directory

<copy todir="E:\opt\myserver\default\deploy">
	<fileset dir="${}/jar" includes="*.jar" />
	<fileset dir="${}/config" includes="*.xml" />


ftp task example: uploads a set of files, via ftp, to a server

<ftp server="" userid="myuser" password="***" action="put">
	<fileset dir="${}" includes="*.ear" />
	<fileset dir="${}/ds/test/oracle" includes="*-ds.xml" />

Advanced Tasks

If you want to write more complex tasks, I suggest two steps:

  1. Read Ant’s documentation. The most important is linked in references
  2. Explore the source code of apache ant,