Ant - How to use a date or timestamp in an Ant build script

Summary: An Ant date and timestamp (tstamp) task example.

I was just digging through some Ant build scripts I've created, and I noticed a segment of a build script that first creates a timestamp, and then uses that timestamp in the process of creating a manifest file. (This build script is used for building a Java Swing application.)

Here's the code from my Ant script that does this timestamp magic:

<!-- run the ant timestamp task -->
<tstamp/>

<!-- use the TODAY variable that is created by tstamp -->
<manifest file="MANIFEST.MF">
  <attribute name="Built-By" value="${manifest.built.by}"/>
  <attribute name="Created-By" value="${manifest.created.by}"/>
  <attribute name="Main-Class" value="${manifest.main.class}"/>
  <attribute name="Implementation-Version" value="${version.number}-b${build.number}"/>   
  <attribute name="Built-Date" value="${TODAY}"/>
  <attribute name="Class-Path" value="${mf.classpath}" />
</manifest>

As you can see, this segment of my Ant build script uses the Ant tstamp task, and then uses a variable named TODAY that is created by this task. Here's a blurb from the tstamp documentation that describes what it does:

tstamp ... sets the DSTAMP, TSTAMP, and TODAY properties in the current project.

By default, the DSTAMP property is in the format "yyyyMMdd", TSTAMP is in the format "hhmm", and TODAY is in the format "MMMM dd yyyy".

So in my case, I'm just using the default timestamp format. I haven't fooled around with any custom timestamp formatting yet, but the tstamp doc page includes the following example, which shows how you can create a property with a different name and timestamp formatting pattern:

<tstamp>
  <format property="TODAY_UK"
          pattern="d-MMMM-yyyy"
          locale="en,UK"/>
</tstamp>

In conclusion, if you ever need to use the date or time in your Ant build scripts, there's no need to write a bunch of code yourself, you can just use the Apache Ant tstamp task.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.