Path element include on condition
ant, eclipse, java
Solution
Without installing Ant-Contrib or similar Ant extensions, you can accomplish what you want with the following 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>
The important part here is what happens in the `-init` target. It depends upon the `-set-path-with-build-dir` and `-set-path-without-build-dir` targets, but Ant only executes one target based upon whether `build.dir.exists` is set or not.
Read more about the Available Task here: http://ant.apache.org/manual/Tasks/available.html.
Problem
All, Can someone please help on how to include a directory in a path element conditionally if the directory exists: So like below ``` <path id="lib.path.ref"> <fileset dir="${lib.dir}" includes="*.jar"/> <path location="${build.dir}" if="${build.dir.exist}" /> </path> ``` This currently does not work because path element does not support the if attribute. In my case, I want to include build.dir if only it exists. Thanks