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は設定されているかどうかに基づいて1つのターゲットのみを実行しbuild.dir.exists
ます。
使用可能なタスクの詳細については、http://ant.apache.org/manual/Tasks/available.htmlを参照してください。