路径元素包含条件

Afa*_*mee 4 java eclipse ant

全部,

如果目录存在,有人可以帮助如何有条件地在路径元素中包含目录:所以像下面

<path id="lib.path.ref">
    <fileset dir="${lib.dir}" includes="*.jar"/>
    <path location="${build.dir}" if="${build.dir.exist}"  />
</path>
Run Code Online (Sandbox Code Playgroud)

这目前不起作用,因为路径元素不支持 if 属性。就我而言,如果仅存在 build.dir,我想包含它。

谢谢

cre*_*ama 5

无需安装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>
Run Code Online (Sandbox Code Playgroud)

这里的重要部分是-init目标中发生的事情。这取决于-set-path-with-build-dir-set-path-without-build-dir目标,但 Ant 仅根据是否build.dir.exists设置来执行一个目标。

在此处阅读有关可用任务的更多信息:http : //ant.apache.org/manual/Tasks/available.html