Antcall 打开一个新的项目范围,但默认情况下,当前项目的所有属性都将在新项目中可用。另外,如果您使用了 =
之类的东西
<antcall target="whatever">
<param name="some.property" value="somevalue"/>
</antcall>
在调用项目中 ${some.property} 也已经设置并且不会被覆盖,因为一旦设置属性在 ant 设计中是不可变的。
或者,您可以将 inheritAll 属性设置为 false,并且只有“用户”属性(在命令行中使用 -Dproperty=value 传递的那些)将传递给新项目。
所以,当 ${some.property} 不是用户属性时,使用 inheritAll="false" 就完成了。
顺便说一句。通过depends="..."属性在目标之间使用依赖比使用antcall更好,因为它打开了一个新的项目范围,并且在新项目中设置的属性不会返回到调用目标,因为它存在于另一个项目范围..
跟一个sn-p,注意区别,先不带inheritAll属性
<project default="foo">
<target name="foo">
<property name="name" value="value1" />
<antcall target="bar"/>
</target>
<target name="bar">
<property name="name" value="value2" />
<echo>$${name} = ${name}</echo>
</target>
</project>
输出:
[echo] ${name} = value1
第二个inheritAll=false
<project default="foo">
<target name="foo">
<property name="name" value="value1" />
<antcall target="bar" inheritAll="false" />
</target>
<target name="bar">
<property name="name" value="value2" />
<echo>$${name} = ${name}</echo>
</target>
</project>
输出:
[echo] ${name} = value2
antcall 的一些经验法则,它很少使用是有充分理由的:
1. 它会打开一个新的项目范围(启动一个新的“ant -buildfile yourfile.xml yourtarget”)
因此它会使用更多内存,从而减慢您的构建速度
2. 被调用目标的依赖目标也会被调用!
3. 属性不会被传递回调用目标
在某些情况下,使用不同的参数调用相同的“独立”目标(一个没有它依赖的目标的目标)以进行重用可能没问题。通常,macrodef 或 scriptdef 用于此目的。因此,在使用 antcall 之前请三思而后行,这也会给您的脚本带来多余的复杂性,因为它与正常流程不符。
在评论中回答您的问题,使用依赖图而不是 antcall
您有一些目标,它包含所有条件并设置适当的属性,目标可以通过 if 和 unless 属性评估这些属性以控制进一步的流程
<project default="main">
<target name="some.target">
<echo>starting..</echo>
</target>
<!-- checking requirements.. -->
<target name="this.target">
<condition property="windowsbuild">
<os family="windows"/>
</condition>
<condition property="windowsbuild">
<os family="unix"/>
</condition>
<!-- ... -->
</target>
<!-- alternatively
<target name="yet.another.target" depends="this.target" if="unixbuild">
-->
<target name="another.target" depends="this.target" unless="windowsbuild">
<!-- your unixspecific stuff goes here .. -->
</target>
<!-- alternatively
<target name="yet.another.target" depends="this.target" if="windowsbuild">
-->
<target name="yet.another.target" depends="this.target" unless="unixbuild">
<!-- your windowspecific stuff goes here .. -->
</target>