Minimal Apache Ant build.xml

Submitted by:Pitter Alderson

Date added:11 September, 2011


This is a minimal apache ant build.xml file that will quickly do all the things that you will generally want to do, including compiling and building a simple Java project to class files and jars. It provides targets to compile the class files and jars and to run the main class. The fields marked "My Project Name Here" and "My Main Class Here" are the fields that need to be modified for your project. To start create a directory for your project with subdirectories "lib" and "src" and place the build.xml file in the same directory. Place the Java sources in the "src" directory. Type "ant build" to compile and "ant run" to test and run the main class.

Tags: apache ant , build xml

Code Snippet:

    <project name="My Project Name Here" basedir="." default="main">

<property name="lib.dir" value="lib"/>

<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>

<property name="src.dir" value="src"/>

<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>

<property name="main-class" value="My Main Class Name Here"/>

<target name="clean">
<delete dir="${build.dir}"/>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${}.jar" basedir="${classes.dir}">
<attribute name="Main-Class" value="${main-class}"/>

<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<path refid="classpath"/>
<path location="${jar.dir}/${}.jar"/>
<arg value="RIMM" />

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>