andersonbd1 andersonbd1 - 1 month ago 22
Java Question

output timestamp in ant

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>

Answer

Use a MacroDef 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 />
Comments