Ant调用定义属性的目标
在Ant中,我想定义一个定义属性的目标(称为Ant调用定义属性的目标,ant,Ant,在Ant中,我想定义一个定义属性的目标(称为a),并从另一个目标(称为B)调用它。我希望目标B在调用目标A后,可以访问目标A中定义的属性 例如: <target name="B"> <antcall target="A" inheritAll="true" inheritRefs="true" /> <echo>${myprop}</echo> </target> <target name="A">
a
),并从另一个目标(称为B
)调用它。我希望目标B
在调用目标A
后,可以访问目标A
中定义的属性
例如:
<target name="B">
<antcall target="A" inheritAll="true" inheritRefs="true" />
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>
${myprop}
但是它不起作用,${myprop}
不打印myvalue
(我想是因为myprop
属性在B
中没有定义)
有什么办法吗?我想你应该用一个参数
<project default="B">
<target name="B">
<antcall target="A">
<param name="myprop" value="myvalue"/>
</antcall>
</target>
<target name="A">
<echo>${myprop}</echo>
</target>
</project>
根据报告:
@alem0lars,既然你说你想细分一个目标,让我提供一个不同的解决方案(不幸的是,这并不能回答你最初的问题) 与其使用
,为什么不让目标B依赖于目标A
<target name="B" depends="A">
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>
${myprop}
另一种方法是将目标重构为宏。您试图使用像函数这样的目标,但它们并不打算这样使用。我通常将大部分逻辑编写为宏,这样我就可以更轻松地将其编写成更复杂的宏。然后我为我需要的命令行入口点编写简单的包装器目标。这是而不是OP想要的。他想在目标A中定义属性,并在目标B中重复。在这里,您在A中定义属性,并在A中重复属性。感谢您澄清问题。您介意检查您的问题吗?您可能不想让B调用B。@rajah9:我想在目标A中定义属性,并在目标B中回显它。目标A是从目标B调用的。主要问题是我想创建一个目标并将其划分为子目标。谢谢。这就是问题所在。您如何以不同的方式实施该行为?我的意思是,我想将目标的逻辑划分为子目标。此外,您可以尝试Ant Contrib包中的任务“AntCallBack”:这是最好的方法。ant属性不是变量,它们是不可变的。对同一build.xml的antcall总是很难闻。
<target name="cond" depends="cond-if"/>
<target name="cond-if" if="prop1">
<antcall target="cond-if-2"/>
</target>
<target name="cond-if-2" if="prop2">
<antcall target="cond-if-3"/>
</target>
<target name="cond-if-3" unless="prop3">
<echo message="yes"/>
</target>
<target name="testCallback" description="Test CallBack">
<taskdef name="antcallback" classname="ise.antelope.tasks.AntCallBack" classpath="${antelope.home}/build" />
<antcallback target="-testcb" return="a, b"/>
<echo>a = ${a}</echo>
<echo>b = ${b}</echo>
</target>
<target name="-testcb">
<property name="a" value="A"/>
<property name="b" value="B"/>
</target>
<project default="mytarg">
<target name="mytarg">
<property name="tgt" value="build"/>
<antcall target="deps"/>
</target>
<target name="deps" depends="aTgt,bTgt"/>
<target name="aTgt">
<echo>"In aTgt doing a ${tgt}"</echo>
</target>
<target name="bTgt">
<echo>"In bTgt doing a ${tgt}"</echo>
</target>
</project>
aTgt:
[echo] "In aTgt doing a build"
bTgt:
[echo] "In bTgt doing a build"
deps:
BUILD SUCCESSFUL
<target name="B" depends="A">
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>