我有一组嵌套的 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>
[底部.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
最佳答案
我认为亚历山大的解决方案很接近。不过,这个怎么样,不需要对 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:
These properties become equivalent to properties you define on the command line. These are special properties and they will always get passed down, even through additional <ant> tasks with inheritall set to false (see above).
关于具有嵌套任务的 Ant 属性集继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3573445/