Path element includes condition

All,

Can anyone help on how to include the directory in the path element conditionally if the directory exists: as shown 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 the path element does not support the if attribute. In my case, I want to enable build.dir, if only it exists.

thank

+3
source share
1 answer

Without installing Ant-Contrib or similar Ant extensions, you can do 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 target -init. It depends on the goals -set-path-with-build-dirand -set-path-without-build-dir, but Ant fulfills only one goal, based on whether it is installed build.dir.existsor not.

: http://ant.apache.org/manual/Tasks/available.html.

+3

All Articles