无需安装Ant-Contrib或类似的 Ant 扩展,您可以使用以下 XML 完成您想要的:
<project default="echo-lib-path">
<property name="lib.dir" value="lib"/>
<property name="build.dir" value="build"/>
<available file="${build.dir}" type="dir" property="build.dir.exists"/>
<target name="-set-path-with-build-dir" if="build.dir.exists">
<echo message="Executed -set-path-with-build-dir"/>
<path id="lib.path.ref">
<fileset dir="${lib.dir}" includes="*.jar"/>
<path location="${build.dir}" />
</path>
</target>
<target name="-set-path-without-build-dir" unless="build.dir.exists">
<echo message="Executed -set-path-without-build-dir"/>
<path id="lib.path.ref">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
</target>
<target name="-init" depends="-set-path-with-build-dir, -set-path-without-build-dir"/>
<target name="echo-lib-path" depends="-init">
<property name="lib.path.property" refid="lib.path.ref"/>
<echo message="${lib.path.property}"/>
</target>
</project>
这里重要的部分是-init
目标中发生的事情。它取决于-set-path-with-build-dir
和-set-path-without-build-dir
目标,但 Ant 仅根据是否build.dir.exists
设置执行一个目标。
在此处阅读有关可用任务的更多信息:http: //ant.apache.org/manual/Tasks/available.html。