4

全て、

ディレクトリが存在する場合、誰かがパス要素にディレクトリを条件付きで含める方法について助けてもらえますか?

<path id="lib.path.ref">
    <fileset dir="${lib.dir}" includes="*.jar"/>
    <path location="${build.dir}" if="${build.dir.exist}"  />
</path>

path要素はif属性をサポートしていないため、これは現在機能しません。私の場合、build.dirが存在する場合にのみそれを含めたいと思います。

ありがとう

4

1 に答える 1

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>

ここで重要なのは、-initターゲットで何が起こるかです。-set-path-with-build-dirとターゲットに依存します-set-path-without-build-dirが、Antは設定されているかどうかに基づいて1つのターゲットのみを実行しbuild.dir.existsます。

使用可能なタスクの詳細については、http://ant.apache.org/manual/Tasks/available.htmlを参照してください。

于 2012-04-30T16:44:14.583 に答える