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="foo.properties"/>

Load different values dependant upon the OS

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

Setup a classpath

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

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}"/>

  </target>

Compile everything under this structure that is a test

  <javac srcdir="${src}"
         destdir="${build}"
         includes="mypackage/p1/**Test"
         classpath="xyz.jar"
         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"/>
    </fileset>
  </copy>
<!-- or when backing up -->
  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
      <globmapper from="*" to="*.bak"/>
  </copy>

Delete some files

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

Run some unit tests

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s