Ant build.xml Example

Writing a Simple Buildfile

Apache Ant (Ant) is a general purpose build tool. Ant is an abbreviation for Another Neat Tool.

Ant is primarily used for building and deploying Java projects but can be used for every possible repetitive tasks, e.g. generating documentation.

Apache Ant's buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.

Here is an Ant build.xml example

<?xml version="1.0" encoding="UTF-8"?>
<project name="coloza-ant" basedir="." default="jar">
  <property name="source.dir" value="src" />
  <property name="lib.dir" value="lib" />
  <property name="class.dir" value="bin" />
  <property name="jar.dir" value="dist" />
  <property name="jar.file" value="${jar.dir}/project-name.jar" />
  <property name="main-class" value="package.Main" />
  <path id="libraries.path">
    <fileset dir="${lib.dir}">
      <include name="*.jar" />
  <target name="clean">
    <delete dir="${class.dir}" />
    <delete dir="${jar.dir}" />
  <target name="compile" depends="clean">
    <mkdir dir="${class.dir}" />
    <javac srcdir="${source.dir}" destdir="${class.dir}" includeantruntime="false"
      classpathref="libraries.path" encoding="UTF-8" /> <!-- encoding used if code has unicode character -->
  <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />
    <mkdir dir="${class.dir}/${lib.dir}" />
    <copy todir="${class.dir}/${lib.dir}" flatten="true">
      <path refid="libraries.path" />
    <manifestclasspath property="manifest.classpath" jarfile="${jar.file}">
      <classpath refid="libraries.path" />
    <jar destfile="${jar.file}" basedir="${class.dir}">
        <attribute name="Main-Class" value="${main-class}" />
        <attribute name="Class-Path" value="${manifest.classpath}" />


Popular posts from this blog