【问题标题】:Alternative to ant stopwatch task蚂蚁秒表任务的替代品
【发布时间】:2012-09-08 17:22:46
【问题描述】:

我在 ant-contrib 中找到了 ant task stopwatch,但我在那里缺少 echo levelduration format 参数。 所以我需要在非安静模式下启动 ant 以获得stopwatch elapsed 输出(此外还看到不需要的 ant echo 消息):

<target name="build">
    <stopwatch name="foo"/>

    <sleep milliseconds="50"/>   <!-- do something -->
    <echo level="info" message="message not wanted to see normally"/>

    <stopwatch name="foo" action="elapsed"/>
</target>

或者,我只看到编写自己的目标或宏,例如

<macrodef name="echo-with-time">
    <attribute name="message" default="" />
    <attribute name="level" default="warning" />
    <sequential>
        <local name="now"/>
        <tstamp><format property="now" pattern="yyyy-MM-dd HH:mm:ss,SSS"/> </tstamp>
        <echo level="@{level}" message="[${now}] @{message}" />
    </sequential>
</macrodef>

问题:有没有办法只输出持续时间(比如用所用时间属性计算)?

【问题讨论】:

    标签: ant time echo stopwatch ant-contrib


    【解决方案1】:

    您可以在要计时的事物的开始和结束处使用自定义的&lt;scriptdef&gt; 读取当前时间戳,然后使用math 任务计算差异。

    类似这样的:

    <var name="start" unset="true"/>
    <var name="stop" unset="true"/>
    <var name="duration" unset="true"/>
    
    <unix-timestamp property="start" />
    <!-- Do something to be timed -->
    <unix-timestamp property="stop" />
    <math result="duration" operand1="${stop}" operation="-" operand2="${start}" datatype="int"/>
    <echo message="Task took ${duration} seconds"/>
    

    其中&lt;unix-timestamp&gt; 是任何受支持语言的&lt;scriptdef&gt;

    <scriptdef name="unix-timestamp" language="javascript">
        <attribute name="property" />
        <![CDATA[
            var property = attributes.get("property");
            var nowSeconds = Math.round(new Date().getTime()/1000);
            project.setProperty(property, nowSeconds);
        ]]>
    </scriptdef>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2011-12-29
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2015-05-30
      相关资源
      最近更新 更多