---
title: "Ant Task的属性及功能"
author: "Caspar"
date: "2009-04-24T15:11:25Z"
tags: []
slug: "ant-task"
draft: false
lastmod: "2009-04-24T15:11:25Z"
id: 374040
url: "https://caspar.iteye.com/blog/374040"
---

（1）antfile属性：在Ant任务中antfile属性的作用是指定要执行的构件文件的名称，可包含路径信息。
例如，在projectA中执行projectB的构件文件。projectA的构件文件内容如下：

<?xml version="1.0"?>
<!-- 在projectA中执行projectB的构件文件   -->
<project name="porjectA" default="callProjectB">
    <target name="callProjectB">
        <echo message="In projectA calling projectB"/>
        <ant antfile="subfile/projectB.xml" />
</target>
</project>
projectB存放于subfile目录下，具体内容如下：

<?xml version="1.0"?>
<project name="porjectB" default="init">
    <target name="init">
        <echo message="In projectB"/>
    </target>
</project>



从执行结果得知，antfile属性的作用相当于在Ant命令中指定-buildfile subfile/projectB. xml选项，只不过Ant任务提供了更方便的功能。如果antfile属性没设定，那么Ant任务会查找名称为build.xml的构件文件。当然antfile属性也可与dir属性结合使用，antfile指定文件名称，dir属性指定文件所在的目录。

（2）dir属性：在Ant任务中dir属性的作用是用于指定构件文件所在的目录。相当于指定了要执行的构件文件的basedir属性。
在默认情况下如果dir属性没设定，那么会以当前执行的构件文件的根目录作为Ant任务的根目录。这个属性设定后，被执行的构件文件的basedir属性将取dir属性的值。
下面把targetA.xml构件文件改写为通过dir属性指定目录以实现同样的功能，具体的构件文件内容如下：

<?xml version="1.0"?>
<project name="porjectA" default="callProjectB">
    <!-- //调用projectB.xml构件文件  -->
    <target name="callProjectB">
        <echo message="In projectA calling projectB"/>
        <ant antfile="projectB.xml"  dir=” subfile” />
    </target>
</project>

（3）target属性：在Ant任务中target属性的作用是指定被执行的构件文件中所要执行的target，如果target属性没有设定，那么将会执行被调用的构件文件中的默认target（也就是project元素中指定default值）。
这个属性相当于在命令行执行时指定要执行的target的选项。例如，在projectA1中调用projectB1，并执行projectB1中的target“target2”。
编写projectA1.xml构件文件，内容如下：

<?xml version="1.0"?>
<project name="porjectA" default="callProjectB">
    <target name="callProjectB">
        <echo message="In projectA calling projectB"/>
         <!-- //通过ant 任务执行projectB.xml构件文件，指定执行target2这个target  -->
        <ant antfile="subfile/projectB.xml" target=" target2"/>
    </target>
</project>

被调用的projectB.xml构件文件的内容如下：

<?xml version="1.0"?>
<project name="porjectB" default="init">
    <target name="init">
        <echo message="In projectB"/>
    </target>
    <target name=”target2” >
        <echo message=”In projectB,execute target2” />
    </target>
</project>

在执行结果中可看出，在targetA.xml中通过Ant任务执行了targetB.xml中的target2这个target。

（4）output属性：在Ant任务中output属性主要用于指定Ant工具执行时的信息输出，可定位到控制台或定位到文件中。
当输出信息到文件中时，相当于在命令行指定-logfile<file>,-l<file>选项。如果指定了dir属性，那么被指定的输出文件的位置是相对于dir属性指定的目录的，当然也可以通过绝对路径指定。
例如，把projectA中的输出信息定位到out.log文件，构件文件的内容如下：

<?xml version="1.0"?>
<project name="porjectA" default="callProjectB">
    <target name="callProjectB">
        <echo message="In projectA calling projectB"/>
         <!-- //输出信息到out.log中  -- >
        <ant antfile="subfile/projectB.xml"  output="out.log"/>
    </target>
</project>

（5）inheritAll属性：在Ant任务中inheritAll属性用于指定被调用的构件文件的project元素中可使用当前project元素中的属性。
这个属性类似于Java程序中类的继承关系。默认时inheritAll属性取值为true。

（6）inheritRefs属性：在Ant任务中，如果设定了inheritRefs属性为true，那么被调用的构件文件中的project元素可引用当前project元素中的reference（参考）任务。

Reference任务的作用是把当前的属性复制到被调用的Ant project中使用。
References任务包含两个属性，如下：

  ● refid属性：这个属性代表当前project中的属性id。

  ● torefid属性：用于指定在被调用的project中的引用id。

Reference的使用例子如下：

<reference refid="path1" torefid="path2"/>

<!-- // 把当前project中的path1属性传递给被调用的project中使用，

在被调用project中通过path2引用这个属性。  -- >