Catalysoft   Turtle
home products articles about us contact us

Recent Articles

What's Good About Clojure?

Clojure is a relatively new language to appear on the Java Virtual Machine (JVM), although it draws on very mature roots in the form of the LISP langu ...

Should You Care About Requirements Engineering?

Recently, I (Adil) was invited to participate in a one day seminar on the subject of Requirements Engineering. Whilst I have no direct experience of t ...

Tips for Setting Up Your First Business Website

To attract all potential customers to your business you need a presence on the web. The problem is that if you haven't set up a website before, you p ...

What's Good about LISP?

LISP is a general-purpose programming language and is the second-oldest programming language still in use, but how much do you know about it? Did you ...

Open Source Tools for Developers: Why They Matter

From a developer's point of view use of open-source tools has advantages beyond the obvious economic ones. With the open-source database MySQL in mind ...

Example of Ant's uptodate Task

Discuss this article >>>

Example of Ant's uptodate task

I've had some difficulty getting ant's uptodate task working for me, so decided to come up with a "bare bones" example to demonstrate how it works.

'Uptodate' Example

First of all, here's the example:
<project default="runtest">

<target name="init">
  <touch file="old.txt"/>
  <sleep seconds="2"/>
  <touch file="new.txt"/>
</target>
 
<target name="check.new.newer">
  <uptodate targetfile="new.txt" srcfile="old.txt" property="new.newer"/>
</target>

<target name="check.old.newer">
  <uptodate targetfile="old.txt" srcfile="new.txt" property="old.newer"/>
</target>

<target name="is.old.newer" if="old.newer">
  <echo message="old is newer than new"/>
</target>

<target name="is.new.newer" if="new.newer">
  <echo message="new is newer than old"/>
</target>

<target name="runtest" 
  depends="init, check.old.newer, check.new.newer, is.old.newer, is.new.newer">
  <echo message="done"/>
</target>

</project>

Description of Targets

The ant script contains the following six targets:
init
Creates two files (assuming they don't already exist) new.txt and old.txt. The file new.txt is created 2 seconds after old.txt so is guaranteed to be newer (hence the name!)
check.new.newer
Uses the uptodate task to see if new.txt is newer than old.txt. If so, then the property new.newer becomes set.
check.old.newer
Uses the uptodate task to see if old.txt is newer than new.txt. If so, (which should not be the case), then the old.newer property becomes set.
is.old.newer
Prints a message if the old.newer property is set
is.new.newer
Prints a message if the new.newer property is set
runtest
Has dependencies that run the targets to initialise, check which of the created files is newer, and print out the messages.

How It Works

I hope it is clear to you what is intended - the files are first created, then checked against each other using the uptodate task to see which is newer, and finally a message (or messages) is printed out according to the conditional targets is.old.newer and is.new.newer. If the script works as expected, then only one of the conditional targets will be run, as only one of the files can be newer than the other, and therefore only one of the two properties tested will actually be set.

Sample Output

When the script is run (by typing ant) the following output is produced:
C:\uptodate-test>ant
Buildfile: build.xml

init:

check.old.newer:

check.new.newer:

is.old.newer:

is.new.newer:
     [echo] new is newer than old
     
runtest:
     [echo] done
     
BUILD SUCCESSFUL
Total time: 2 seconds
This is exactly what we would hope to see - the file new.txt has been shown to be newer than the file old.txt!

Summary

This is a good example because it demonstrates the setting, and not setting, of properties by the uptodate task, as well as the conditional execution of tasks based on whether properties have been set. The structure shown can form a basis for more complex scripts that use uptodate in conjunction with other tasks to perform conditional compilation or conditional distribution of code.

Discuss this article >>>


Simon White