Output several timestamps in ant

前端 未结 5 1433
温柔的废话
温柔的废话 2020-12-15 18:53

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-ta

相关标签:
5条回答
  • 2020-12-15 19:21

    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 />
    
    0 讨论(0)
  • 2020-12-15 19:21

    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
    
    0 讨论(0)
  • 2020-12-15 19:39

    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 />
    
    0 讨论(0)
  • 2020-12-15 19:40

    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).

    <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>
    
    0 讨论(0)
  • 2020-12-15 19:42

    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
    
    0 讨论(0)
提交回复
热议问题