How-to: Get timestamp in ANT scripts

We use a lot of ANT scripts to build our archives. And archives without version number/timestamp is a big pain. We faced a lot of issues mainly integration issues. This is a small piece of script which can be used to add timestamp to an archive’s name.

This is done using the tstamp task in ANT. tstamp is highly configurable and uses the java.text.SimpleDateFormat date/time patterns.

<target name="timestamp.target">
	<tstamp>
		<format property="current.time"
			pattern="yyyyMMdd_HHmmss" />
		<format property="archive.name"
			pattern="'MyArchive_'yyyyMMdd_HHmmss'.jar'" />
	</tstamp>
	<echo>${current.time}</echo>
	<echo>${archive.name}</echo>
</target>

I got the following output when I tried to run the above build script:

Buildfile: /home/subin/sampleProject/build.xml
timestamp.target:
     [echo] 20101013_103329
     [echo] MyArchive_20101013_103329.jar
BUILD SUCCESSFUL
Total time: 524 milliseconds

So, we have tstamp, which formats (format) the time/date information and assigns the result to the specified property. The echo statements demonstrate how we can use those values further.

Hope this helps. Have a nice day.

Advertisements