Writing a Simple Ant Build file

Writing a Simple Ant Build file

Apache Ant’s buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.

Projects:

A project has three attributes – all three are not mandatory

– name the name of the project.
– default the default target to use when no target is supplied. since Ant 1.6.0, every project includes an implicit target that contains any and all top-level tasks and/or types.
– basedir the base directory from which all path calculations are done. This attribute might be overridden by setting the “basedir” property beforehand.

Each project defines one or more targets. A target is a set of tasks you want to be executed. When you don’t specify any target , the project’s default is used.

Targets:

A target can depend on other targets. You might have a target for compiling, a target for creating a distributable and so on. Generally you can only build a distributable when you have compiled first, so the distribute target depends on the compile target. Ant resolves these reference dependencies.

Tasks:

A task is a piece of code that can be executed.

A task can have multiple attributes (or arguments, if you prefer). The value of an attribute might contain references to a property. These references will be resolved before the task is executed.

Tasks have structure like below:
<name attribute1=”value1″ attribute2=”value2″ … />
where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute.

Properties:

Properties are an important way to customize a build process or to just provide shortcuts for strings that are used repeatedly inside a build file. In its most simple form properties are defined in the build file or might be set outside Ant.

This is done by placing the property name between “${” and “}” in the attribute value.

Example Buildfile

<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>

<target name=”init”>
<!– Create the time stamp –>
<tstamp/>
<!– Create the build directory structure used by compile –>
<mkdir dir=”${build}”/>
</target>

<target name=”compile” depends=”init”
description=”compile the source ” >

<javac srcdir=”${src}” destdir=”${build}”/>
</target>

<target name=”dist” depends=”compile”
description=”generate the distribution” >
<!– Create the distribution directory –>
<mkdir dir=”${dist}/lib”/>

<!– Put everything in ${build} into the MyProject-${DSTAMP}.jar file –>
<jar jarfile=”${dist}/lib/MyProject-${DSTAMP}.jar” basedir=”${build}”/>
</target>

<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *