【问题标题】:ANT parameter passing by referenceANT 参数通过引用传递
【发布时间】:2011-02-16 01:53:06
【问题描述】:


我对 ANT 脚本非常陌生,并且正在使用它来自动化我项目中的日常构建。我更多地使用它来编写脚本(XML 文件)并聚合预先存在的功能并提出构建过程。

我觉得我对 antcall/target 概念有一些基本的理解问题。特别是当使用参数进行antcall时,如C++,有没有办法调用目标将参数作为通过引用传递?以便调用者可以检索目标中更改的值?

在下面的例子中,我想检查两个文件是否相同并显示结果,但是对于下面的例子,我会得到输出


是相同的文件:${isFileName}


例子:

< target name="checkFileAreSame">
< condition property="isFileSame">
< filesmatch file1="a.txt" file2="b.txt"/>
< /condition >
< /target >

< target name="Maintask">
< antcall target="checkFileAreSame">
< param name="isFileSame" value="false">
< /antcall >
< echo message="Are files Same : ${isFileSame}"/>
< /target >


提前感谢您的意见。

【问题讨论】:

    标签: apache ant parameter-passing


    【解决方案1】:

    Ant properties are immutable - 一旦设置,它们的值就不能改变了。

    使用antcall task 时请注意:

    被调用的目标在一个新的 项目;请注意,这意味着 属性、引用等由设置 被调用的目标不会持久化 到调用项目。

    但还要注意,您使用“antcall”param 属性传递给被调用目标的属性在被调用目标中是不可变的。 这意味着在给出的“条件”任务示例中:

    <condition property="isFileSame">
        <filesmatch file1="a.txt" file2="b.txt"/>
    </condition>
    

    isFileSame 属性已被调用者设置为 false,因此无论文件比较如何都将保持为 false。

    declare dependencies between targets 以这种方式更常见:

    <target name="checkFileAreSame">
        <condition property="isFileSame">
            <filesmatch file1="a.txt" file2="b.txt"/>
        </condition>
    </target>
    
    <target name="Maintask" depends="checkFileAreSame">
        <echo message="Are files Same : ${isFileSame}"/>
    </target>
    

    Ant 将确定“Maintask”的调用图要求首先运行“checkFileAreSame”。

    【讨论】:

    【解决方案2】:

    听起来您需要来自 ant-contrib 包的 AntCallBack 任务(与核心 Ant 分开)。

    如果您(像许多人一样)对 Ant 的“声明式”流样式不满意,Ant-contrib 是一个非常有用的库。

    看这里:

    http://ant-contrib.sourceforge.net/

    http://ant-contrib.sourceforge.net/tasks/tasks/index.html

    http://ant-contrib.sourceforge.net/tasks/tasks/antcallback_task.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2011-08-21
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多