|
3.8. Setting TimestampsThe 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.
|
|