Thursday, March 31, 2011

apache ant build files

Hi,

I am trying to output the timestamp after each step (target). When excuting the below build file by ant testtime i get the output:

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

Execting the build file below:



  <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" />

Why do I get always the same timestamp ?

From stackoverflow
  • Once evaluated, TODAY_UK will not be computed again.

    May be you should add at the beginning of your task something like:

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

    Using the Core Task tstamp


    You have this script to test in order to configure, then display a timestamps:

    <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>
    
  • You get always the same timestamp because the property containing this value is evaluate only once. The proposition to add a '' at the beginning of the task will not help either because Ant will not update the value of the property.

    The solution I came up with is to define your own Ant task:

    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();
        }
    }
    

    Then define an Antlib file (e.g. message-antlib.xml) to instantiate and invoke the class:

    <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>
    

    modify your build.xml file accordingly

    <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>
    

    to get the following output:

    [tsecho] 21:46:20 
    [tsecho] 21:46:20 start
    [tsecho] 21.46 end
    
  • why dont you use the antContrib ant listener check this out

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

0 comments:

Post a Comment