Ant Basics
 

Each 'Project' has a Build File, Default build file name is 'build.xml', Can Specify any name with '-buildfile' command line option, Ant's buildfiles are written in XML.

The first or root element of any buildfile is always the <project> tag. No buildfile can be without one nor can it have more than one.

<project name="MyProject" default="all" basedir=".">
...
</project>

The <project> tag has three attributes: name, default, and basedir.

  • The name attribute gives the project a name.
  • The default attribute refers to a target name within the buildfile. If you run Ant without specifying a target on the command line, Ant executes the default target. If the default target doesn't exist, Ant returns an error.
  • The basedir attribute defines the root directory of a project. Typically, it is ".", the directory in which the buildfile resides, regardless of the directory you're in when you run Ant. However, basedir can also define different points of reference.

Each buildfile contains one project and at least one (default) target. Examples are: 'compile', 'test', 'install', 'clean', etc.
<target name="dosomething">
  <task1 param1="value1" param2="value2">
  <task2 param3="value3" >
  ...
</target>

The <target> tag has three attributes: name, depends, if, unless, descriptiondefault, and basedir.

  • The name attribute gives the target a name.
  • The depends attribute are a comma-separated list of names of targets on which this target depends.
  • The if attribute the name of the property that must be set in order for this target to execute.
  • The unless attribute the name of the property that must not be set in order for this target to execute.
  • The description attribute a short description of this target's function.

Targets must have a name and may have several additional attributes that determine when and if the target actually gets executed. The target is made up of one or more Tasks like invoke a command or another program. Targets can have Dependencies, examples: 'install' depends on 'compile', Targets can handle cascading dependencies, each Dependency is handled only once, dependency executed only if required.

It should be noted, however, that Ant's depends attribute only specifies the order in which targets should be executed - it does not affect whether the target that specifies the dependency(s) gets executed if the dependent target(s) did not (need to) run.

Ant tries to execute the targets in the depends attribute in the order they appear (from left to right). Keep in mind that it is possible that a target can get executed earlier when an earlier target depends on it.A target gets executed only once, even when more than one target depends on it .

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 a common structure:
<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.

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. Each Task is bound to a Java class file that Ant executes, passing to it any arguments or sub-elements defined with that task. The Ant tool is extensible and it allows you to create your own tasks

Typical build.xml Tasks
init, sets properties, prepare, creates directories, build, builds the system, package, creates jar file, install, installs an application to Tomcat or other engine, deploy, deploy a WAR engine, reload, update previously installed application engine, redeploy.


Properties

A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.

Built-in Properties
Ant provides access to all system properties as if they had been defined using a <property> task. For example, ${os.name} expands to the name of the operating system. For a list of system properties see the Javadoc of System.getProperties.
In addition, Ant has some built-in properties:

basedir the absolute path of the project's basedir (as set with the basedir attribute of <project>).
ant.file the absolute path of the buildfile.
ant.version the version of Ant
ant.project.name the name of the project that is currently executing; it is set in the name attribute of <project>.
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.1", "1.2", "1.3" and "1.4".

 
 

Ant
Installing Ant
Ant Basics
Using Ant
Running Ant
Ant Tasks
Core Tasks
Optional Tasks
Ant Command-Line

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions