Ant日志中的时间戳?

Tig*_*ger 11 java ant timestamp

是否有一种简单的方法让Ant记录器(默认或其他)为每条消息添加时间戳?

我能想到的唯一方法是使用Log4jListener并使其设置包含时间戳.或者编写一个自定义记录器,它将DefaultLogger子类化并写入时间戳.如果有更好或更简单的方法(最好不要求用户在他们的Ant lib目录中安装新的jar文件),

我有兴趣听到它.

Gav*_*rke 10

鉴于属性在ant中是不可变的,你需要做一些有点时髦的事情,否则你最终会一次又一次地记录相同的时间戳.

使用antcall为您提供了一个全新的会话,这意味着您可以重复使用该属性,尽管它有点笨拙.

<macrodef name="timestamp.echo"> 
  <attribute name="message"/>    
  <sequential> 
    <antcall target="_timestamp.echo">
        <param name="message" value="@{message}" />
    </antcall>
  </sequential> 
</macrodef>  


<target name="_timestamp.echo"> 
   <tstamp> 
    <format property="current.time" pattern="dd/MM/yyyy hh:mm:ss"/> 
   </tstamp>          
   <echo message="${current.time} ${message}"/> 
</target>
Run Code Online (Sandbox Code Playgroud)

如果您使用Ant 1.8,那么您可以使用更清洁的本地

<macrodef name="timestamp.echo"> 
  <attribute name="message"/>    
  <sequential> 
   <local name="current.time" />
   <tstamp> 
    <format property="current.time" pattern="dd/MM/yyyy hh:mm:ss"/> 
   </tstamp>          
   <echo message="${current.time} @{message}" />
  </sequential> 
</macrodef>  
Run Code Online (Sandbox Code Playgroud)

以下是如何使用它

<target name="testTsEcho" depends="init" description="blah">
    <timestamp.echo message="test" />
    <sleep seconds="10" />
    <timestamp.echo message="test2" />
</target>
Run Code Online (Sandbox Code Playgroud)


小智 7

试试这个

ant -logger org.apache.tools.ant.listener.ProfileLogger

它打印每个目标的进入时间和退出时间以及每个目标所用的时间(以毫秒为单位).


Ric*_*ler 6

您可以定义Ant宏定义来设置当前时间戳,然后每次需要在整个build.xml中引用它时调用macrodef

以下macrodef将时间戳设置为属性(如果要自定义其设置的属性,可以向macrodef添加属性):

<macrodef  name="set.timestamp">
  <sequential>
    <tstamp>
      <format property="current.time" pattern="MM/dd/yyyy hh:mm"/>
    </tstamp>
  </sequential>
</macrodef>
Run Code Online (Sandbox Code Playgroud)

然后使用它,只需根据需要访问macrodef设置的属性:

<target name="doFoo" depends="dir.check" if="dir.exists">
  <set.timestamp/>
  <!--in this example, just echo the timestamp -->
  <echo message="${current.time}"/>
</target>
Run Code Online (Sandbox Code Playgroud)

有关ant macrodef的更多信息,请查看文档.

  • 这会将时间戳存储在属性中,因此所有消息将共享相同的时间.在某些情况下很好,但是对于例如看到构建的哪个部分花费太长时间是无用的.由于OP提到"每条消息的时间戳",我实际上并不认为这是对问题的正确答案. (3认同)