The Ant buildfile snippet below is an attempt to simply output the time before and after each sql script is run. I cannot change the structure of the Ant targets (create-tables must call run-sql-script just as it does). The problem is that the properties (time and time2) are immutable (http://ant.apache.org/manual/Tasks/property.html) and thus only time the first operation and not the second. Is there no way to do what I'm trying to do in Ant?
  <target name="create-tables">
    <antcall target="run-sql-script">
      <param name="db.script" value="teams.sql"/>
    </antcall>
    <!-- Create the base UDM schema. -->
    <antcall target="run-sql-script">
      <param name="db.script" value="players.sql"/>
    </antcall>
  </target>
  <target name="run-sql-script">
    <tstamp>
      <format property="time" pattern="MM/dd/yyyy hh:mm:ss aa"
          offset="-5" unit="hour"/>
    </tstamp>
    <echo>before: ${time}</echo>
    <sql
        classpath="${classpath}"
        driver="${db.driver}"
        url="${db.url}"
        userid="${db.userid}"
        password="${db.password}"
        src="${script.dir}/${db.script}"
        delimiter="${script.delimiter}"
        onerror="abort">
    </sql>              
    <tstamp>
      <format property="time2" pattern="MM/dd/yyyy hh:mm:ss aa"
            offset="-5" unit="hour"/>
    </tstamp>
    <echo>after: ${time2}</echo>
  </target>
Use a <macrodef> task together with the <local> task (introduced in Ant 1.8):
<macrodef name="echotimestamp">
  <sequential>
    <local name="timestamp" />
    <tstamp>
      <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss" />
    </tstamp>
    <echo message="${timestamp}" />
  </sequential>
</macrodef>
<echotimestamp />
Update: You can use an antcall to invoke a task, and create/echo a new timestamp within the scope of that call.
This example shows how to pass a message to the call and echo the current timestamp with a message:
<target name="timestamp2">
  <tstamp>
    <format property="current.time" pattern="MM/dd/yyyy hh:mm:ss aa" />
  </tstamp>
  <echo message="${message} ${current.time}" />      
</target>
<target name="test">
  <antcall target="timestamp2">
    <param name="message" value="hello" />
  </antcall>
  <sleep seconds="5"/>
  <antcall target="timestamp2">
    <param name="message" value="world" />
  </antcall>
</target>
The output when this is run is:
test:
timestamp2:
     [echo] hello 09/24/2009 05:33:22 PM
timestamp2:
     [echo] world 09/24/2009 05:33:24 PM
Following on from @Niek's answer, we can build a macro that behaves like echo but with a time stamp
<macrodef name="echoTS">
  <attribute name="message"/>
  <sequential>
    <var name="current.time" unset="true"/>
    <tstamp><format property="current.time" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>
    <echo message="${current.time}> @{message}" />
  </sequential> 
</macrodef>
<target name="test-timestamp">
  <echoTS message="hi" />
</target>
which will give output
test-timestamp:
     [echo] 2013-05-03 12:02:38> hi
I like the macrodef solution if indeed it is more efficient than the target one, but I use a var unset=true to force a resetting of the variable, like:
<macrodef name="echoTimestamp">
   <sequential>
      <var name="current.time" unset="true"/>
         <tstamp>
            <format property="current.time" pattern="yyyy-MM-dd HH:mm:ss" />
         </tstamp>
         <echo message="${current.time}" />
   </sequential> 
</macrodef>
Usage
<echoTimestamp />
<sleep seconds="3"/>
<echoTimestamp />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With