Tuesday, April 8, 2008

Timestamp dependent task in Ant

Sometimes we need to do certain task in Ant that only need to be run if certain file or files has been updated. javac task has time dependency built-in already, but other tasks do not. Good thing we have ant-contrib library which has outofdate tasks.

Recently we have a need to do some manipulation on the content of an EAR file which require the EAR file to be expanded first. We do not want to keep expanding the EAR file if there is no update on the EAR file itself. This is a perfect situation to utilize the ant-contrib's outofdate task in combination with built-in ant's touch task.

So we have setup like this:
<target name="extract-source">
<ac:outofdate>
<sourcefiles>
<pathelement path="${app.name}.ear"/>
</sourcefiles>
<targetfiles path="${input.dir}/extract.ts"/>
<sequential>
<unjar src="${app.name}.ear"
dest="${input.dir}"
overwrite="yes"/>
<touch file="${input.dir}/extract.ts"/>
</sequential>
</ac:outofdate>
</target>

Where we have the timestamp of the ear file compared against a timestamp file that we generate/touch after the ear expansion. So if at any time there is a new ear file which is newer then it was last expanded we'll expand it again else we just go on with our life.

With so many built-in tasks in Ant, I wonder why a lot of the ant-contrib tasks are not merged into ant already.

No comments: