0

In one of your topic, named "output timestamp in ant", i've tested the 2 way explained in order to display several time in the same Ant script.

But, this 2 solutions don't work for me, Ant give me all the time the same date/time.

Anybody can explaine me why ?

I've found a solution (but note very perfet to my mind ) :

<tstamp>
    <format property="date.etape1" pattern="dd/MM/yyyy HH:mm:ss" />
</tstamp>
<echo message="${date.etape1} bla bla bla...." />
<tstamp>
    <format property="date.etape2" pattern="dd/MM/yyyy HH:mm:ss" />
</tstamp>
<echo message="${date.etape2} bla bla bla...." />

If somebody can explain me how make work one of the 2 solutions of the topic "output timestamp in ant", i would be very happy !!!

4

4 回答 4

1

根据 TSTAMP 来源,它应该可以工作(来源在这里) Date 对象总是被实例化。也许表面后面正在进行一些奇怪的任务回收?

我正在尝试使用这个 ant 构建文件

<project name="testTStamp" default="xxx" basedir=".">
    <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>

    <target name="default"> 
        <tstamp> 
            <format property="current.time_one" pattern="MM/dd/yyyy hh:mm:ss.SSS aa"/> 
        </tstamp> 
        <echo message="RUN_TIME: ${current.time_one}"/> 
        <sleep seconds="5"> </sleep> 
        <tstamp> 
            <format property="current.time_two" pattern="MM/dd/yyyy hh:mm:ss.SSS aa"/> 
        </tstamp> 
        <echo message="RUN_TIME: ${current.time_two}"/> 
    </target> 

    <target name='xxx' depends='default, testMe' />
</project>

好的,如果在默认目标中,我确实更改了格式属性的名称,它可以工作。所以后面有一些缓存魔法。

于 2012-04-17T09:41:15.627 回答
0

Ant 中的属性是不可变的;它们不是变量。

<local>但是,您可能可以通过使用该任务来实现您想要的。此任务导致之后在target(或sequential,或任何其他块)中设置的属性仅在本地指定。

因此,如果您所有的时间戳回声都在单独的目标中,那么它将起作用。在单个目标中,您仍然需要使用多个属性,例如“time_start”、“time_end”等。

于 2012-07-09T16:26:48.487 回答
0

在跟进这个线程时,我读到macrodef 是一种更合适的方法,因为它在每次调用时都会不断实例化,所以没有缓存问题。我已经简化了他们的例子。我包含了我的整个文件,因此您可以将下面的代码保存为 clock.xml 并直接从 ant 运行它。

<project name="marktime" basedir=".">
<description>
    logstamp is a simple macrodef for ANT to echo a time stamp out to an open log.
    It is not written to pass back the current time, just simple echo to stdout.
            You are welcome to add which file to output the results to, add blurbs to messages.

</description>

<macrodef name="logstamp">
  <sequential>
  <local name="logtime"/>
  <tstamp>
    <format property="logtime" pattern="yyyy.MM.dd : HH:mm:ss z"/>
  </tstamp>
  <echo  message=" ### Current time now: ${logtime}"/>
  </sequential>
</macrodef>

<target
name="init"
description="boilerplate for all targets">
    <!-- Just run the Macrodef -->
<logstamp/>
<sleep seconds="5"> </sleep> 
<logstamp/>
<sleep seconds="5"> </sleep> 
<logstamp/>
<sleep seconds="5"> </sleep> 
<logstamp/>
<sleep seconds="5"> </sleep> 
<logstamp/>
</target>
</project>

结果如下:

C:\desktop\>ant -f clock.xml init
Buildfile: C:\desktop\clock.xml

init:
     [echo]  ### Current time now: 2014.02.14 : 16:41:06 PST
     [echo]  ### Current time now: 2014.02.14 : 16:41:11 PST
     [echo]  ### Current time now: 2014.02.14 : 16:41:16 PST
     [echo]  ### Current time now: 2014.02.14 : 16:41:21 PST
     [echo]  ### Current time now: 2014.02.14 : 16:41:26 PST

BUILD SUCCESSFUL
Total time: 20 seconds
于 2014-02-15T00:36:06.663 回答
0

根据 Bee Kay 的回答,此宏通过避免使用“本地”,而是在“宏定义”上使用“属性”来与 ANT 1.7 版一起使用:

<project name="echotime" basedir="." default="echotime">
<description>
echotime is a simple macrodef for ANT to echo a time stamp to an open log.
It is not written to pass back the current time, just simple echo to stdout.
</description>

<macrodef name="echotime">
    <attribute name="logtime" default="" />
    <sequential>
        <tstamp>
            <format property="logtime" pattern="yyyy.MM.dd HH:mm:ss z" />
        </tstamp>
        <echo message="Build finished at ${logtime}" />
    </sequential>
</macrodef>

<target name="echotime">
    <echo message="${ant.version}" />
    <echotime />
</target>

结果如下:

>ant -buildfile echotime.xml
Buildfile: echotime.xml

echotime:
     [echo] Apache Ant version 1.7.1 compiled on June 27 2008
     [echo] Build finished at 2019.12.19 11:43:29 EST

BUILD SUCCESSFUL
Total time: 0 seconds
于 2019-12-19T16:50:44.300 回答