tags:

views:

1406

answers:

2

The ant script 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/CoreTasks/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>
+4  A: 

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
Rich Seller
-1 That doesn't give a new timestamp for every call.
Aaron Digulla
@Aaron you are quite right, I have an example of how to do this correctly somewhere, I'll dig it out and update
Rich Seller
updated, now with added working
Rich Seller
@Rich - what did you change? what was the difference between Aaron's original and your change?
andersonbd1
@andersonbd1, in the first instance the macrodef will set a property with the timestamp, once the timestamp is set it is used everywhere in the build with the original value. In the second version the timestamp variable is scoped to the inner call, so will have a new value each time
Rich Seller
A: 

I found that if you use it as a macro rather than an ant target, it works better since it doesn't loop thru the ant file from the beginning everytime you do a "antcall target=" (less check if you have dependencies and property sets).

eddie
<target name="testMe"> <MyTimestamp></MyTimestamp> <sleep seconds="5"></sleep> <MyTimestamp></MyTimestamp></target><macrodef name="MyTimestamp"> <sequential > <tstamp> <format property="current.time" pattern="MM/dd/yyyy hh:mm:ss aa"/> </tstamp> <echo message="RUN_TIME: ${current.time}"/> </sequential></macrodef>
eddie