ANT – usefully boring

Ant the industry standard build tool is used by nearly every java developer at some point.

One writes Ant targets in xml that are then executed to compile classes, copy files, sign jars, run unit tests… you name it there is probably an ant target that does it.

For some reason XML markup, catchy as it is, never sticks in my mind but here are snippets of ANT targets I use a lot ….

Load values from this properties file

  <property file=""/>

Load different values dependant upon the OS

  <!-- set a property dependant upon the os -->
  <condition property="propertyFile"
             else="${unixPropertyFileName}" >
      <os family="windows"/>

Setup a classpath

    <pathelement path="${classpath}"/>
    <pathelement location="lib/helper.jar"/>
  <!-- Or using filesets and dirsets as well -->
    <pathelement path="${classpath}"/>
    <fileset dir="lib">
      <include name="**/*.jar"/>
    <pathelement location="classes"/>
    <dirset dir="${build.dir}">
      <include name="apps/**/classes"/>
      <exclude name="apps/**/*Test*"/>
    <filelist refid="third-party_jars"/>

Compile everything under this structure

  <target name="compile" depends="init"
                description="compile the source " >

    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>


Compile everything under this structure that is a test

  <javac srcdir="${src}"
         debug="on" />

Copy a file to somewhere

  <copy file="myfile.txt" tofile="mycopy.txt"/>
<!-- or -->
  <copy todir="../dest/dir">
    <fileset dir="src_dir">
      <exclude name="**/*.java"/>
<!-- or when backing up -->
  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
      <globmapper from="*" to="*.bak"/>

Delete some files

  <!-- file -->
  <delete file="/lib/ant.jar"/>
  <!-- dir -->
  <delete dir="lib"/>
  <!-- set -->
    <fileset dir="." includes="**/*.bak"/>

Run some unit tests

    <test name="my.test.TestCase"/>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s