Ant - Build Files

What is build file

Ant uses an xml file for its configuration. The default file name is build.xml. Typically, Ant's build file, build.xml should live in the project's base directory. But you are free to use other file names or place the build file in some other location.

Let us create our first task.create a file called build.xml anywhere in your computer and copy the following lines into this file.I have created this file in D:\antexample


<?xml version="1.0"?>
 <project default="hello">
    <target name="hello">
    <echo message="Hello, World"/>
  </target>
</project>

Importance of build tool

Before learning Apache Ant, one must understand the need for a build tool. Why do we need Ant, or more specifically, why do we need a build tool?

The "Build" is a process that covers all the steps required to create a "deliverable" of your software. In the Java world, this typically includes:

  1. Generating sources (sometimes).
  2. Compiling sources.
  3. Compiling test sources.
  4. Executing tests (unit tests, integration tests, etc).
  5. Packaging (into jar, war, ejb-jar, ear).
  6. Running health checks (static analyzers like Checkstyle, Findbugs, PMD, test coverage, etc).
  7. Generating reports.

Open the command prompt and navigate to the folder where the build.xml resides, and type ant.You should see the following output:

Ant Installation

Now we've got a working build file, let's take a closer look at it's contents:

  • Project :-The project is the root element of the build file, it contains one or more targets. The default attribute is required and specifies the default build target (in this case: "hello").
  • Target :- A target represents a project milestone in ant, it contains zero or more tasks. The name attribute is required and specifies the name of the target (in this case: "hello").
  • Task (echo in this case) :- Tasks are the smallest units of work in ant. Tasks may operate on many files, but the same operation will be applied to each file.
    The echo task's message attribute specifies the text that is generated by this task..

For my install I used the following values.

  • "ANT_HOME" : D:\apache-ant-1.9.3
  • "JAVA_HOME" : C:\Program Files (x86)\Java\jdk1.6.0_14
  • "PATH" : C:\Program Files (x86)\Java\jdk1.6.0_14\bin

A first Ant build

This example will show you how to use Ant to compile and run a Java program,by using ant.Follow the steps mentioned below for this sample example.

Step 1: Creating the project directory

Create a directory and name it to antfirstexample in any of your local drive.Everything will go inside this directory like: source files, created output files, and the build file. All new Java/Ant projects should start this way.

Step 2 : Create Java Class inside this directory

Now create a java class Test.java and add following code into this file

Student.java
package com.example;

public class Test {
	public static void main(String args[]) {
		for (int i = 0; i < 10; i++) {
			System.out.println(i);
		}
	}
}

Step 3 : Write build file(build.xml)

We are going to write Ant Script to compile the program.build.xml file is mentioned below.

build.xml

<?xml version="1.0"?>
<project name="MyProject" default="compile" >
<target name="compile">
<javac srcdir="." />
<echo>compilation complete!</echo>
</target>
</project>

By convention, all Ant build files should be called build.xml, unless you need two or more build files to be in the same directory

If you're not familiar with XML syntax, I recommend you read up a little on it first, so you know what elements, attributes, tags, closing tags, and comments look like. It's very similar to HTML syntax. But more formal You know XML? Great. Let's break this down line-by-line:

-> The <project> element is always the root element in Ant build files.
-> This example containing two attributes, name and default.
-> The <target> element is a child of <project>.
-> The <target> element contains two child elements: <javac> and <echo>.

The graphical view of the XML Tree of the build file is given below.The graphical view of the XML tree makes it easier to look at a build file, and so the structure of the build file should become more clear.

Ant Installation

At the top of the tree is a element, which has two attributes, name and default. All Ant build files must contain a single element as the root element. It tells Ant the name of the project and, optionally, the default target.

Under the <project> element there is a child <target> with the name compile. A target represents a single stage in the build process. A build file can have many targets, each of which must have a unique name.

The build file’s compile target has two XML elements, one is <javac> and other <echo>.javac calls the javac compiler to compile Java source; the other echoes a message to the screen.The compilation task has one attribute, srcdir, which is set to “.” and which tells the task to look for source files in and under the current directory. The second task, <echo>, has a text child node that will be printed when the build reaches it.

Step 4 : Running the build

Open the command prompt and navigate to the folder where the build.xml resides, and type ant.If the build file has been typed correctly, then you should see the following output on cmd:

Ant Installation

In the above screen Ant has compiled the single Java source file in the current directory and printed a success message after that.





comments powered by Disqus