Ant antcall定义属性的目标

ale*_*ars 19 ant

在Ant中,我想定义一个目标(被称为A),它定义一个属性,antcall并从另一个目标(被称为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>
Run Code Online (Sandbox Code Playgroud)

但是它不起作用而且<echo>${myprop}</echo>不打印myvalue(我认为因为属性myprop没有定义B).

有没有办法做到这一点?

SOU*_*ser 11

根据Apache Ant FAQ:

    <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>
Run Code Online (Sandbox Code Playgroud)

Note: <antcall> tasks do not pass property changes back up to the environment they were called from, so you wouldn't be able to, for example, set a result property in the cond-if-3 target, then do <echo message="result is ${result}"/> in the cond target.

在这方面,使用antcall 无法做你想做的事.

========== 编辑 ===========

尝试antcallback:AntCallBack与标准的'antcall'任务相同,只是它允许在被调用目标中设置被调用目标中的属性.
http://antelope.tigris.org/nonav/docs/manual/bk03ch20.html

从上页粘贴的示例代码:

    <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>
Run Code Online (Sandbox Code Playgroud)


Kon*_*hik 9

另一种方法是将目标重构为宏.您正在尝试使用类似函数的目标,但它们并不打算以这种方式使用.我通常将我的大部分逻辑写成宏,这样我就可以更容易地将它组成更复杂的宏.然后我为我需要的命令行入口点编写简单的包装器目标.

  • 这是最好的方法.ant属性不是变量,它们是不可变的.一个antcall进入同一个build.xml _always_闻起来很糟糕. (2认同)

Mad*_*sen 5

而不是使用<antcall>,为什么不让目标B依赖目标A

<target name="B" depends="A">
    <echo>${myprop}</echo>
</target>
<target name="A">
    <property name="myprop" value="myvalue" />
</target>
Run Code Online (Sandbox Code Playgroud)