如何为变量赋值并重用Ant [英] How to assign value to variable and reuse it Ant

查看:227
本文介绍了如何为变量赋值并重用Ant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆