【问题标题】:Ant propertyset inheritance with nested tasks带有嵌套任务的 Ant 属性集继承
【发布时间】:2011-04-04 03:49:37
【问题描述】:

我有一组嵌套的 Ant 构建文件,我需要控制每个“子”任务继承哪些属性。我试图将它们定义为属性集(以保持代码可管理),但与属性不同,这些不会被子任务继承。

下面的例子演示了这个问题,foo.* 被复制到中间项目而不是底部项目。如果我将每个属性定义为显式继承,例如bar.*,它们也会被底层项目继承。

有没有办法让一组属性一直继承下去,就像单个属性一样?在不重写子流程的情况下,还有什么我可以尝试的吗?

[top.xml]

<?xml version="1.0"?>
<project name="test-top">
    <property name="foo.1" value="1"/>
    <property name="foo.2" value="2"/>
    <property name="bar.1" value="1"/>
    <property name="bar.2" value="2"/>

    <ant antfile="middle.xml" inheritall="false">
        <propertyset>
            <propertyref prefix="foo."/>
        </propertyset>
        <property name="bar.1" value="${bar.1}"/>
        <property name="bar.2" value="${bar.2}"/>
    </ant>
</project>

[middle.xml]

<?xml version="1.0"?>
<project name="test-middle">

    <echo>foo ${foo.1} ${foo.2}</echo>
    <echo>bar ${bar.1} ${bar.2}</echo>

    <ant antfile="bottom.xml" inheritall="false"/>
</project>

[bottom.xml]

<?xml version="1.0"?>
<project name="test-bottom">

    <echo>foo ${foo.1} ${foo.2}</echo>
    <echo>bar ${bar.1} ${bar.2}</echo>

</project>

[ant -f top.xml 的输出]

 [echo] foo 1 2
 [echo] bar 1 2
 [echo] foo ${foo.1} ${foo.2}
 [echo] bar 1 2

【问题讨论】:

    标签: ant build-process build-automation


    【解决方案1】:

    我认为 Alexander 的解决方案很接近。不过这样怎么样,不需要对 middle.xml 或 bottom.xml 进行任何更改。

    这个想法是使用echoproperties 任务将属性集“展开”到各个属性,然后在ant 任务调用中使用它。

    在调用 middle.xml 之前,使用类似这样的方式编写设置的属性:

    <echoproperties destfile="myproperties.txt">
        <propertyset>
            <propertyref prefix="foo."/>
            <propertyref prefix="bar."/>
        </propertyset>
    </echoproperties>
    

    然后调用 middle.xml:

    <ant antfile="middle.xml" inheritall="false">
        <property file="myproperties.txt" />
    </ant>
    

    如你所说,提供给ant任务inherit all the way down的属性,所以你只需要更改top.xml:

    这些属性等同于 您在命令中定义的属性 线。这些是特殊的属性和 他们总是会被传承下去,即使 通过额外的 ant> 任务 inheritall 设置为 false(见上文)。

    【讨论】:

    • 感谢马丁和亚历山大。我想知道将属性写入文件,但没有明确重新列出我想要传递的所有属性的任何方法(即,不比上面bar.* 的解决方案更好)。我错过了“echoproperties”任务,因为它在 Ant 文档中被列在“可选任务”而不是“核心任务”下......现在我知道了!
    【解决方案2】:

    在 top.xml 中,您可以使用&lt;propertyfile&gt; 任务创建具有可继承属性的文件。

    然后您可以在每个子模块中使用&lt;property file="..."/&gt;加载此文件。

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2023-04-05
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多