Section 3.8. Setting Timestamps


3.8. Setting Timestamps

The tstamp task sets properties holding the current time so you can time stamp your builds. This task creates the DSTAMP (day stamp), TSTAMP (time stamp) 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". If you use this task, it's almost invariably run in an initialization target.

You can see how this works in Example 3-5, which stores the build number and creation date in the project's .jar file, using buildnumber and tstamp.

Example 3-5. Using the build number and tstamp tasks (ch03/buildnumber/build.xml)
<?xml version="1.0" ?> <project default="main">     <property name="message" value="Building the .jar file." />     <property name="src" location="source" />     <property name="output" location="bin" />     <target name="main" depends="init, compile, compress">         <echo>             ${message}         </echo>     </target>        <target name="init">         <buildnumber/>         <tstamp/>         <delete dir="${output}"/>         <mkdir dir="${output}" />     </target>        <target name="compile">         <javac srcdir="${src}" destdir="${output}" />     </target>      <target name="compress">         <jar destfile="${output}/Project.jar" basedir="${output}"              includes="*.class" >             <manifest>             <attribute name="Author" value="${user.name}"/>             <section name="Shared">                 <attribute name="Title" value="Example"/>                 <attribute name="Vendor" value="MegaAntCo"/>                 <attribute name="Build" value="${build.number}"/>                 <attribute name="Date" value="${TODAY}"/>             </section>             <section name="Copyright">                 <attribute name="Copy" value="(C) MegaAntCo 2005"/>             </section>         </manifest>      </jar>   </target> </project>

Here's the resulting manifest file from the JAR file, including build number and creation date:

Manifest-Version: 1.0 Ant-Version: Apache Ant 1.6.1 Created-By: 1.4.2_03-b02 (Sun Microsystems Inc.) Author: Steven Holzner Name: Shared Title: Example Vendor: MegaAntCo Build: 3 Date: June 10 2005 Name: Copyright Copy: (C) MegaAntCo 2005

This task has only one attribute, prefix, which is optional and sets a prefix for the DSTAMP, TSTAMP, and TODAY properties. For example, if prefix="time", these properties will be time.TSTAMP, time.TSTAMP, and time.TODAY, allowing you to name the properties created by this task yourself (at least to the extent of calling giving them names like name.TSTAMP instead of just TSTAMP).

The tstamp task supports a format nested element that lets you set the date and time format. Here's an example that creates a timestamp in the property timestamp:

  <tstamp>       <format property="timestamp" pattern="MM/dd/yyyy hh:mm:ss"/>   </tstamp>

You can see the attributes of the format element in Table 3-12.

The date/time patterns used by format are the same as used by the Java SimpleDateFormat class. For more info on those patterns, see http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html.


Table 3-12. The format task's attributes

Attribute

Description

Required

locale

Specifies the locale for the date/time string. Make this of the language, country, variant. The possible values are defined in the Java Locale class.

No

offset

Specifies the offset you want to add to or subtract from the current time, if any.

No

pattern

Specifies the date/time pattern you want to use. The possible values are defined in the Java SimpleDateFormat class.

Yes

property

Specifies the property in which you want to store the date/time string.

Yes

timezone

Specifies the time zone used for generating the time. Possible values are defined in the Java TimeZone class.

No

unit

Specifies the unit of the offset you've specified in offset. Possible values are millisecond, second, minute, hour, day, week, month, or year.

No




    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net