AntCall Task:执行过程中调用并执行其他target

AntCall 任务的作用是允许在一个target的执行过程中调用并执行其他的target。例如,在打包项目前需要对项目进行编译,那么可以在打包项目的target中通过AntCall任务使得编译的target先执行。当然这种情况也可以通过target间设置depends属性来实现。AntCall任务必须在target元素内执行,这个任务不能在target元素外执行。

4.2.1  AntCall Task属性及功能

AntCall 任务主要包括target,inheritAll和inheritRefs 3个属性,具体说明如下:

(1)target属性:在AntCall任务中target属性的作用是指定要被调用执行的target,通过名称指定这个target属性是必需的。值得注意的是,当通过AntCall任务调用的target存在依赖的target(depends中指定了target),则depends属性中被指定的target也会被执行。

(2)inheritAll属性:用于指定是否继承当前的属性。默认时为true,代表被调用的target可使用这些属性。

(3)inheritRefs属性:用于指定是否覆盖reference属性或者是否建立一个对当前reference属性的引用。在默认的情况下,AntCall任务不会覆盖reference属性,除非把inheritRefs属性设为true。默认时inheritRefs属性为false。

4.2.2  利用AntCall Task实现target间调用的实例

利用AntCall任务来实现target间的相互调用。下面编写构件文件antcallSample.xml,实现在targetA中调用执行targetB。构件文件内容如下:

<?xml version="1.0"?>

<project name="antcallSample" default="targetA">

    <target name="targetA">

        <echo message="In targetA calling targetB"/>

        <!-- //调用targetB  -->

<antcall target="targetB" >

        </antcall>

        <echo message="After call targetB" />

    </target>

    <target name="targetB" >

        <echo message="In targetB" />

    </target>

</project>

保存构件文件,然后在命令行执行命令ant –f antcallSample.xml,执行结果如图4.3    所示。

图4.3  antcallSample.xml

上面的例子的作用是在执行targetA的过程中执行targetB。如果targetB中设定了depends属性,则depends属性中指定的target也会被执行。修改antcallSample.xml如下,以演示depends的依赖关系:

<?xml version="1.0"?>

<project name="antcallSample" default="targetA">

    <target name="init" >

        <echo message="In init target"/>

    </target>

    <target name="targetA">

        <echo message="In targetA calling targetB"/>

        <antcall target="targetB" >

        </antcall>

        <echo message="After call targetB" />

    </target>

    <target name="targetB" depends="init">

        <echo message="In targetB" />

    </target>

</project>

保存构件文件,然后再次执行ant –f antcallSample.xml命令,执行结果如图4.4所示。

图4.4  antcallSample.xml命令的执行结果

上面的执行结果表明:在执行targetA时调用targetB,由于targetB依赖于init target。因此init target先于targetB执行。

4.2.3  利用AntCall Task实现target间调用时传递参数的实例

当需要从一个target传递参数到被调用的target时,可以使用<param> 类型进行传递。当然也可以在target中定义property来实现,与Java中的方法调用时传递参数相似。修改antcallSample.xml以实现传递参数的功能,内容如下:

<?xml version="1.0"?>

<project name="antcallSample" default="targetA">

    <target name="init" >

        <echo message="In init target"/>

    </target>

    <target name="targetA">

        <echo message="In targetA calling targetB"/>

          <!-- //通过property传递  -->

        <property name="prop" value="prop property" /> 

        <antcall target="targetB" >

               <!-- // 通过antcall设定param实现  -->

            <param name="path1" value="path1 param" /> 

        </antcall>

        <echo message="After call targetB" />

    </target>

    <target name="targetB" depends="init">

        <echo message="In targetB" />

        <echo message="path1=${path1}" />

        <echo message="prop=${prop}" />

    </target>

</project>

再次执行ant –f antcallSample.xml命令,执行结果如图4.5所示。

图4.5  antcallSample.xml

从执行结果可看出,通过property指定和通过AntCall中的param指定的属性都传递到targetB中。对于param 类型只有两个属性:name和value。由于AntCall任务中的inheritAll属性默认时为true,所以property能被targetB引用。如果targetB中也定义了相同的property,那么可以通过设置inheritRefs属性和reference类型进行覆盖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值