使用ANT任务时将文件传递给antfile

时间:2014-07-30 17:56:01

标签: java xml ant

例如,我有两个build.xml:build.xml和subbuild.xml。在build.xml中,它将使用ant任务来调用并运行subbuild.xml,如下所示:

<target name="antCall1" >
    <ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>

我的问题是这样的:我在build.xml旁边有一个“test.xml”。如何修改上面的代码,以便我可以将“test.xml”传递给subbuild.xml并在subbuild.xml中执行一些操作?

实际上,我想将“test.xml”作为subbuild.xml中的任务传递。我很困惑如何让subbuild.xml访问build.xml中的“test.xml”。谢谢!

1 个答案:

答案 0 :(得分:3)

在调用build.xml之前,只需在subbuild.xml中设置包含文件名称的属性即可。使用ant任务调用的Ant子项目会自动继承其父项中设置的所有属性,除非属性inheritAll设置为false。或者,您可以使用CAustin在评论中建议的嵌套property元素将属性传递给子项目。

第一种方法:

<target name="antCall1" >
    <!-- since the file is in the same directory as the build.xml -->
    <property name="input.file" value="test.xml" />
    <ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>

第二种方法:

<target name="antCall1" >
    <ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml">
        <property name="input.file" value="test.xml" />
    </ant>
</target>

subbuild.xml

<loadfile property="file.contents" srcFile="${input.file}"/>
<echo message="${file.contents}" />