apache ant构建文件

时间:2009-02-12 12:05:20

标签: apache ant build

我正在尝试在每个步骤(目标)之后输出时间戳。当通过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>

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

3 个答案:

答案 0 :(得分:3)

评估后,将不再计算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>

答案 1 :(得分:0)

您始终获得相同的时间戳,因为包含此值的属性仅评估一次。在任务开始时添加''的提议也无济于事,因为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

答案 2 :(得分:0)

为什么不使用antContrib ant侦听器检查这个

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