【问题标题】:apache ant build filesapache ant 构建文件
【发布时间】:2009-02-12 12:05:20
【问题描述】:

我试图在每一步(目标)之后输出时间戳。当通过 ant testtime 执行以下构建文件时,我得到输出:

Buildfile: build.xml

testdepend1:
     [echo] ****** Start: test depend 1 ******
     [echo] ****** Finish: test depend 1******
     [echo] 02/12/2009 11:58:07 AM

testdepend2:
     [echo] ****** Start: test depend 2 ******
     [echo] ****** Finish: test depend 2******
     [echo] 02/12/2009 11:58:07 AM

testdepend3:
     [echo] ****** Start: test depend 3 ******
     [echo] ****** Finish: test depend 3******
     [echo] 02/12/2009 11:58:07 AM

执行下面的构建文件:

<?xml version="1.0" ?>
<!-- ==================================================================== -->
<!-- Sample Propagation Ant Script -->
<!-- ==================================================================== -->
<project name="Portal Propagation Ant Tasks" basedir="." default="usage">
    <tstamp>  
     <format property="TODAY_UK" pattern="MM/dd/yyyy hh:mm:ss aa" locale="en,UK" unit="second"/>  
    </tstamp>  

    <target name="testdepend1" description="test depend1">
     <echo message="****** Start: test depend 1 ******" />
       <sleep seconds="10"/>
     <echo message="****** Finish: test depend 1******" />
      <echo>${TODAY_UK}</echo>   
    </target>

    <target name="testdepend2" description="test depend2">
     <echo message="****** Start: test depend 2 ******" />
      <sleep seconds="10"/>
     <echo message="****** Finish: test depend 2******" />
      <echo>${TODAY_UK}</echo>   
    </target>

    <target name="testdepend3" description="test depend3">
     <echo message="****** Start: test depend 3 ******" />
     <sleep seconds="10"/>
     <echo message="****** Finish: test depend 3******" />
      <echo>${TODAY_UK}</echo>   
    </target>

    <target name="testtime" depends="testdepend1, testdepend2, testdepend3" description="output a timestamp" />

</project>

为什么我总是得到相同的时间戳?

【问题讨论】:

    标签: apache ant build


    【解决方案1】:

    一旦评估,TODAY_UK 将不再计算。

    您可能应该在任务开始时添加如下内容:

    <tstamp>
      <format property="TODAY_UK" pattern="dd MMM yyyy HH.mm" locale="en_GB" />
    </tstamp> 
    <echo message="${TODAY} at ${TSTAMP}" />
    

    使用核心任务tstamp


    您有this script 进行测试以进行配置,然后显示时间戳:

    <project name="tstamp_demo" basedir="." default="display">
        <target name="display" depends="tstamp" description="TSTAMP demo">
            <echo>DSTAMP: ${DSTAMP}</echo>
            <echo>TSTAMP: ${TSTAMP}</echo>
            <echo>TODAY: ${TODAY}</echo>
            <echo>TODAY_UK: ${TODAY_UK}</echo>
        </target>
    
    
        <target name="tstamp" description="Set DSTAMP/TSTAMP/TODAY, plus whatever in the body">
            <tstamp>
                <format property="TODAY_UK" pattern="d-MMMM-yyyy" locale="en,UK"/>
            </tstamp>
        </target>
    
        <target name="display.start" depends="tstamp.start" description="TSTAMP demo with prefix">
            <echo>start.DSTAMP: ${start.DSTAMP}</echo>
            <echo>start.TSTAMP: ${start.TSTAMP}</echo>
            <echo>start.TODAY: ${start.TODAY}</echo>
        </target>
    
        <target name="tstamp.start">
            <tstamp prefix="start"/>
        </target>
    </project>
    

    【讨论】:

      【解决方案2】:

      您总是得到相同的时间戳,因为包含此值的属性只计算一次。在任务开头添加 '' 的提议也无济于事,因为 Ant 不会更新属性的值。

      我想出的解决方案是定义你自己的 Ant 任务:

      public class StampedEcho extends Echo {
      
          private String message;
          private String pattern = "d-MMMM-yyyy";
          private Locale locale = Locale.US;
      
          public void setPattern(String pattern) { this.pattern = pattern; }
      
          public void setLocale(String locale) { this.locale = new Locale(locale); }
      
          @Override
          public void setMessage(String message) { this.message = message; }
      
          @Override
          public void execute() throws BuildException {
              String date = new SimpleDateFormat(this.pattern, this.locale).format(new Date());
              super.setMessage(date + (this.message == null ? "" : " " + this.message));
      
              super.execute();
          }
      }
      

      然后定义一个 Antlib 文件(例如message-antlib.xml)来实例化和调用类:

      <antlib>
          <macrodef name="tstamp">
              <attribute name="message" default=""/>
              <attribute name="pattern" default="HH:mm:ss"/>
              <attribute name="locale" default="en"/>
              <sequential>
                  <!-- I suppose that the class is in Ant's classpath -->
                  <taskdef name="tsecho" classname="StampedEcho"/>
      
                  <tsecho message="@{message}" pattern="@{pattern}" locale="@{locale}"/>
              </sequential>
          </macrodef>
      </antlib>
      

      相应地修改您的 build.xml 文件

      <project xmlns:msg="antlib:message">
          <typedef file="message-antlib.xml" uri="antlib:message"/>
      
          <target name="testing">
              <msg:tstamp/>
              <msg:tstamp message="start"/>
              <msg:tstamp message="end" pattern="HH.mm" locale="en_GB"/>
          </target>
      </project>
      

      得到以下输出:

      [tsecho] 21:46:20 
      [tsecho] 21:46:20 start
      [tsecho] 21.46 end
      

      【讨论】:

        【解决方案3】:

        你为什么不使用 antContrib 蚂蚁监听器看看这个

        http://neopatel.blogspot.com/2009/09/timestamp-your-ant-build.html

        【讨论】:

          猜你喜欢
          • 2011-03-23
          • 2019-06-15
          • 2010-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-05
          • 1970-01-01
          相关资源
          最近更新 更多