问题描述
我是 Ant 的新手,我有一个场景来分配我已经得到的当前时间 [1],同时创建一个文件夹 [2] 并在文件中添加一些文件到文件夹 [3].所以我需要获得我在 [2] 中获得的时间价值.我基本上是一个 Java 人,如果是在 Java 中,那么拥有一个全局变量并重新使用它是几秒钟的工作.但在这里我不确定如何在不同的目标标签中全局重用该值.请分享您对此的看法.
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]:这里我无法获得与上面 [2] 相同的 current.time 值
[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 中,任何不在目标中的任务都会在任何目标之前执行.因此,您所要做的就是将您的属性 current.time
设置在任何目标之外,并且该属性将可用于您的所有目标:
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>
在上面,时间戳是在第一次执行这个 build.xml
时设置的,因为它不在任何目标中.现在,时间戳可用于所有目标.
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.
顺便说一下,我设置了属性 ${results.dir}
以便在 StackOverflow 中更容易阅读,否则目录名称会超出页面边缘.
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.
这篇关于如何为变量赋值并重用它 Ant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!