Ant build file

Ant build file:

Apache ant uses an xml file for the configuration. Default name is build.xml and should be put into the project base directory.

Commonly used elements of ant build file:

1. Project:

It is the root element of the build.xml file. A project element has the following attributes:
a. name: It specifies the name of the project.
b. default: It specifies the default target for the build script.
c. basedir: It specifies the base directory of the project.

2. Property:

Property element is used to define the properties like project name, project source directory etc. A property can be read from a file or a classpath resource using file and resource attributes respectively. The value of the property can be accessed using ${propertyName}.
Commonly used ant predefined properties:
1. ant.file: It specifies the build file location.
2. ant.version: It specifies the installed ant version.
3. basedir: It specifies the base directory of the build.
4. ant.project.name: It specifies the name of the project.
5. ant.home: It specifies the home directory of apache ant installation.
6. ant.core.lib: It specifies the ant jar file location.
7. ant.java.version: It specifies the java version which is used by ant.

3. Task:

A task is an atomic unit of work.
Commonly used ant task:
1. javac: It compiles the specified source code.
2. zip: It creates a zip file.
3. jar: It creates a jar file.
4. war: It creates a war file.
5. ear: It creates an ear file.
6. junit: It runs the junit test cases from the junit framework.
7. echo: It prints the text to the System.out or to a file.
8. javadoc: It generates the code documentation.
9. sql: It executes the specified sql statements.
10. mkdir: It creates the specified directory structure.
11. path: It creates a classpath container which can be used later.

4. Target:

A target is used to group the tasks and can be run directly via ant. A target can be dependent on other target. If a target depends on other one or more targets then ant will automatically executes the dependent targets.
Example: If a target t1 depends on target t2 and ant instructed to executed t1. In this case ant first executes t2 and then t1 because of the dependency.
Commonly used attributes of target elements:
a. name: It specify the target name.
b. depends: It specify the target names on which this target depends.

build.xml file example

<?xml version="1.0" encoding="UTF-8"?>
<!-- Set the project name, basedir and default target to be executed-->
<project name="Ant-Build-Project-Test" default="mainTarget" basedir=".">
<!-- Sets the properties here-->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
 
<!-- Target for deleting the existing directories-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
 
<!-- Target for creating the new directories-->
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
 
<!-- Target for compiling the java code-->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
 
<!-- Defualt target to run all targets-->
<target name="mainTarget" depends="compile">
<description>Main target</description>
</target>
 
</project>
 

No comments: