How to assign value to variable and reuse it Ant

2019-05-10 23:01发布

I am new to Ant, I have a scenario to assign a current time which I have got[1], while creating a folder[2] and down the file I add some file to the folder[3]. So there I need to get the value of time which I have got in [2]. I am basically a java guy, if it was in java it was few seconds job to have one global variable and re-using it. But here I am not sure how to reuse the value globally in different target tags. Kindly share your thought on this.

[1]

<macrodef  name="set.timestamp">
      <sequential>
         <tstamp>
            <format property="current.time" pattern="MM-dd-yyyy_hh-mm-ss"/>
        </tstamp>
      </sequential>
   </macrodef>

[2]

<target name="init" depends="setRuntimeArchive">
      <set.timestamp/>
      <mkdir dir="${results}/${classname}_${current.time}/xml" />
      <mkdir dir="${results}/${classname}_${current.time}/html" />
      <mkdir dir="${junit-report-output}" />
   </target>

[3]: Here I am not able to get current.time value as same as I got above [2]

<target name="runTestResults">
      <copy
         file="${eclipse-home}/${report}.xml"
         tofile="${results}/${classname}_${current.time}/xml/${report}_${platform}.xml"
         failonerror="false" />
      <xslt
         style="${etf-home}/plugins/${org.eclipse.test}/JUNIT.XSL"
         basedir="${results}/${classname}_${current.time}/xml"
         destdir="${results}/${classname}_${current.time}/html" />
      <antcall target="runTestStatus" />
   </target>

标签: ant
1条回答
Melony?
2楼-- · 2019-05-10 23:09

In Ant, any task not in a target is executed before any targets. Therefore, all you have to do is set your property current.time outside of any targets, and that property will be available for all of your targets:

<project name="foo" default="some.task" basedir=".">
    <tstamp>
        <format property="current.time"
             pattern="MM-dd-yyyy_hh-mm-ss"/>
    </tstamp>

    <target name="run.test.status"
         depends="run.test.results">
         ...
    </target>

    <target name="run.test.results">
         <property name="results.dir" 
             value="${results/${classname_$current.time}/xml"/>
         <mkdir dir="${results.dir}"/>
         <copy
             file="${eclipse-home}/${report}.xml"
             tofile="${results.dir}/${report}_${platform}.xml"
             failonerror="false" />
         <xslt
             style="${etf-home}/plugins/${org.eclipse.test}/JUNIT.XSL"
             basedir="${results}/${classname}_${current.time}/xml"
             destdir="${results}/${classname}_${current.time}/html" />
   </target>

In the above, the time stamp is set when this build.xml is first executed since it's not in any target. Now, the Timestamp is available in all targets.

By the way, I set the property ${results.dir} to make it easier to read in StackOverflow since the directory name would otherwise extend beyond the edge of the page.

查看更多
登录 后发表回答