Ant Projects

Jakob Jenkov
Last update: 2015-08-18

An Ant project consists of at least one Ant build script. Inside the Ant build script there will typically be some configuration of the project and some targets which perform part of the building of the project the Ant build script belongs to.

Remember, an Ant project is really just a software project (typically a Java project) that uses Ant as a build tool. The very same software project could also be built with Maven or Gradle.

An Ant build script can include or execute other Ant build scripts. Thus you can split your Ant project into multiple Ant build scripts. How this is done is explained in later texts in this tutorial.

The project Element

The root XML element of an Ant build script is the project element. Here is an example Ant build script containing just the project element:

<project>
    
</project>

Inside the project element you can set a few attributes which impact the behaviour of your Ant build script. These attributes will be covered in the following sections.

name

The name attribute can be used to set the name of your project. The name is not necessarily used by Ant, but it can be very helpful for developers reading your build script to know what project it belongs to. Here is an example of setting the name attribute of the project element:

<project name="MyProject">
    
</project>

default

The default attribute is used to specify what Ant target should be executed by default if no Ant target is provided to Ant on the command line. When you run Ant without specifying a target to execute, Ant will execute the default target. Here is an example project element which contains a default attribute:

<project name="MyProject" default="build">
    
  <target name="build">
      <echo>The default target</echo>
  </target>    
    
</project>

In this example the default attribute contains the value build. This means that the target named build is the default target. The example also contains a target named build just so you can see how the default attribute value points to the name of the default target.

To run the default target you would use this command:

ant

This will run Ant. Since no target name is specified as command line argument to the ant command, Ant will execute the default target.

basedir

The basedir attribute of the project element can be used to specify what directory is to be used as base directory when resolving relative file paths. Any relative file path specified inside your Ant build script will be interpreted as being relative to the base directory. Here is an example project element with the basedir attribute set:

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

This example sets the basedir attribute to . . The . means the same directory as the build script is located in.

If no basedir is specified then Ant will use the root directory of the project, meaning the directory where the Ant build script is located.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next