Version 1.2
Copyright © 2008, 2009, 2010, 2011 Lars Vogel
22.11.2011
| Revision History | ||
|---|---|---|
| Revision 0.1 | 30.11.2008 | Lars Vogel |
| Created | ||
| Revision 0.2 - 1.2 | 12.01.2009- 22.11.2011 | Lars Vogel |
| bugfixes and enhancements | ||
Table of Contents
Apache Ant (Another Neat Tool) is a build tool, mainly for Java projects. A build tool can be used to automate certain repetitive tasks, e.g. compiling source code, running software tests, creating jar files, javadocs, etc.
A build process typically includes:
Ant uses a xml file for its configuration. This file is usually called "build.xml". Within this build file you specify the targets for ant. A target is a step which ant will perform. You also can specific dependencies. If target A depends on target B, ant will first do B and then A. Also you specify the main target. This target is the target ant will try to execute per default. If this target depends on other targets then ant will automatically perform these task first and so on and so on.
Download Apache Ant from http://ant.apache.org/.
Extract the zip file into a directory structure of your choice. Set the "ANT_HOME" environment variable to this location and include the "ANT_HOME/bin" directory in your path.
Make sure that also the JAVA_HOME environment variable is set to the JDK. This is required for running Ant.
Check your installation by opening a command line and typing "ant -version" into the commend line. The system should find the command ant and show the version number of your installed ant.
The following describes how you compile Java classes, create an executable .jar file and create Javadoc for your project with Apache Ant.
Create a Java Project "de.vogella.build.ant.first" in Eclipse. Create a package called "math" and the following class.
package math; public class MyMath { public int multi(int number1, int number2) { return number1 * number2; } }
Create the package "test" and the following class.
package test; import math.MyMath; public class Main { public static void main(String[] args) { MyMath math = new MyMath(); System.out.println("Result is: " + math.multi(5, 10)); } }
Create a new File through File -> New -> File and call the file "build.xml"
Implement this code to your build.xml.
<?xml version="1.0"?> <project name="Ant-Test" default="main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="build.dir" location="build" /> <property name="dist.dir" location="dist" /> <property name="docs.dir" location="docs" /> <!-- Deletes the existing build, docs and dist directory--> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${docs.dir}" /> <delete dir="${dist.dir}" /> </target> <!-- Creates the build, docs and dist directory--> <target name="makedir"> <mkdir dir="${build.dir}" /> <mkdir dir="${docs.dir}" /> <mkdir dir="${dist.dir}" /> </target> <!-- Compiles the java code (including the usage of library for JUnit --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}"> </javac> </target> <!-- Creates Javadoc --> <target name="docs" depends="compile"> <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}"> <!-- Define which files / directory should get included, we include all --> <fileset dir="${src.dir}"> <include name="**" /> </fileset> </javadoc> </target> <!--Creates the deployable jar file --> <target name="jar" depends="compile"> <jar destfile="${dist.dir}\de.vogella.build.test.ant.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="test.Main" /> </manifest> </jar> </target> <target name="main" depends="compile, jar, docs"> <description>Main target</description> </target> </project>
The code is documented, you should be able to determine the purpose of the different ant tasks via the documentation in the coding.
Run the build.xml file as an "Ant Build".

After this process your data structure should look like this:

Congratulation! You have successfully used Apache Ant from Eclipse.
Ant allows to create classpath containers and use them in tasks. The following "build.xml" from project "de.vogella.build.ant.classpath" demonstrates this.
<?xml version="1.0"?> <project name="Ant-Test" default="Main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="lib.dir" location="lib" /> <property name="build.dir" location="build" /> <!-- Create a classpath container which can be later used in the ant task --> <path id="build.classpath"> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> </path> <!-- Deletes the existing build directory--> <target name="clean"> <delete dir="${build.dir}" /> </target> <!-- Creates the build directory--> <target name="makedir"> <mkdir dir="${build.dir}" /> </target> <!-- Compiles the java code --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" /> </target> <target name="Main" depends="compile"> <description>Main target</description> </target> </project>
You can use the following statements to write the full classpath to the console. You can use this to easily verify if the classpath is correct.
<!-- Write the classpath to the console. Helpful for debugging --> <!-- Create one line per classpath element--> <pathconvert pathsep="${line.separator}" property="echo.classpath" refid="junit.class.path"> </pathconvert> <!-- Write the result to the console --> <echo message="The following classpath is associated with junit.class.path " /> <echo message="${echo.classpath}" />
Apache Ant allows to run JUnit tests. Ant defines the "junit" task. You only need to include the junit.jar and the compiled classes into the classpath for Ant and then you can run Junit tests. See JUnit Tutorial for an introduction into JUnit.
The following is a JUnit test for the previous example.
package test; import math.MyMath; import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyMathTest { @Test public void testMulti() { MyMath math = new MyMath(); assertEquals(50, math.multi(5, 10)); } }
You can run this JUnit unit test via the following build.xml. This example assumes that the JUnit jar "junit.jar" is in folder "lib".
<?xml version="1.0"?> <project name="Ant-Test" default="main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="build.dir" location="build" /> <!-- Variables used for JUnit testin --> <property name="test.dir" location="src" /> <property name="test.report.dir" location="testreport" /> <!-- Define the classpath which includes the junit.jar and the classes after compiling--> <path id="junit.class.path"> <pathelement location="lib/junit.jar" /> <pathelement location="${build.dir}" /> </path> <!-- Deletes the existing build, docs and dist directory--> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${test.report.dir}" /> </target> <!-- Creates the build, docs and dist directory--> <target name="makedir"> <mkdir dir="${build.dir}" /> <mkdir dir="${test.report.dir}" /> </target> <!-- Compiles the java code (including the usage of library for JUnit --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}"> <classpath refid="junit.class.path" /> </javac> </target> <!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain--> <target name="junit" depends="compile"> <junit printsummary="on" fork="true" haltonfailure="yes"> <classpath refid="junit.class.path" /> <formatter type="xml" /> <batchtest todir="${test.report.dir}"> <fileset dir="${src.dir}"> <include name="**/*Test*.java" /> </fileset> </batchtest> </junit> </target> <target name="main" depends="compile, junit"> <description>Main target</description> </target> </project>
Eclipse has an ant editor which make the editing of ant file very easy by providing syntax checking of the build file. Eclipse has also a ant view. In this view you execute ant files via double-clicking on the target.

Apache Ant allows to convert relative paths to absolute paths. For example.
<!-- Location of a configuration --> <property name="my.config" value="../my-config.xml"/> <makeurl file="${my.config}" property="my.config.url"/>
This property can later be used for example as a parameter.
<param name="highlight.xslthl.config" expression="${my.config.url}"/>
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.de Google Group. I have created a short list how to create good questions which might also help you.
http://www.vogella.de/code/codejava.html Source Code of Examples