Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output several timestamps in ant

Tags:

timestamp

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>
like image 232
andersonbd1 Avatar asked Sep 24 '09 15:09

andersonbd1


4 Answers

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 />
like image 22
Arieh Avatar answered Sep 18 '22 19:09

Arieh


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
like image 129
Rich Seller Avatar answered Sep 17 '22 19:09

Rich Seller


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
like image 41
Jamie Cook Avatar answered Sep 18 '22 19:09

Jamie Cook


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 />
like image 41
Niek Avatar answered Sep 20 '22 19:09

Niek