1

ディレクトリをある場所から別の場所に移動する必要があるシナリオがあります。宛先フォルダーに同じディレクトリが存在する場合は、ディレクトリ名を oldname_1 に変更する必要があります。だから私は次のようにスニペットを書きました:

<target name="Move">
    <IF>
        <available file="${output.dir}" type="dir" />
        <then>
            <echo message="Directory exists" />
            <rename src="${output.dir}" dest="${output.dir}_1"/>
            <property name="newdirectory" value="${dest}"/>
        </then>
    <ELSE>
        <echo message="Directory does not exist" />
    </ELSE>
    </IF>
        <move file="${newdirectory}" todir="C:\reports" />
    </target>

私が得ているエラーは次のとおりです。

問題: タスクの作成に失敗しました
原因: 名前が定義されていません。
処置: スペルを確認してください。
処置: カスタム・タスク/タイプが宣言されていることを確認してください。
処置: /宣言が行われたことを確認してください。
4

2 に答える 2

2

Ian Robertsが既に述べたように、Ant-Contrib jar が必要で、<taskdef/>この jar を指すように をセットアップします。プロジェクト内に配置し、バージョン管理システムにチェックインすることを強くお勧めします。このようにして、誰かがあなたのプロジェクトをチェックアウトするとき、彼らは既に Ant-Contib.jar をインストールしています。

私の標準は、ビルドに必要なすべてのオプションの jar (コンパイルに必要な jar ではない) をディレクトリ${basedir}/antlibに配置し、次にオプションの各 jar を独自のディレクトリに配置ant-contrib-1.0b3.jarすること${basedir}/antlib/antcontribです。

次に、タスクを次のように定義します。

<property name="antlib.dir"      value="${basedir}/antlib"/>

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${antlib.dir}/antcontrib"/>
    </classpath>
</taskdef>

このように、jar ファイルを新しいバージョンの Ant-Contrib jar に更新する場合は、それをディレクトリにプラグインするだけです。を更新する必要はありませんbuild.xml

net/sf/antcontrib/antlib.xmlまた、私は and を使用しないことに注意してくださいnet/sf/antcontrib/antcontrib.propertiesXMLファイルを使用する必要があります。この手順はタスク ページにあり、メイン ページのインストール手順とは異なります。その理由は、XML ファイルには<for>タスクの正しい定義が含まれていますが、プロパティ ファイルには含まれていないためです。


ただし、 Ant 1.9.1 には、オプションの jar ファイルを必要ifとしない別の方法があります。unlessこれらは新しいIfおよびUnlessエンティティ属性です

これらはすべてのタスクまたはサブエンティティに配置でき、通常は Ant-Contrib のif/elseものを置き換えることができます。

<target name="move">
    <available file="${output.dir}" type="dir"
        property="output.dir.exists"/>
    <echo message"Directory exists"
        if:true="output.dir.exists"/>
    <move file="${output.dir}" tofile="${output.dir}_1"
        if:true="output.dir.exists"/>
    <property name="newdirectory" value="${dest}"
        if:true="output.dir.exists"/>
    <echo message="Directory does not exists"
        unless:true="output.dir.exists"/>
    <move file="${newdirectory}" todir="C:\reports" />
</target>

あなたの例ほどきれいではありません。ただし、代わりにターゲット名にif=andunless=パラメータを使用します。

<target name="move.test">
    <available file="${output.dir}" type="dir"
        property="output.dir.exists"/>
</target>

<target name="move"
    depends="move.test, move.exists, move.does.not exists">
    <move file="${newdirectory}" todir="C:\reports" />
</target>

<target name="move.exists"
    if="output.dir.exists">
    <echo message="Directory exists" />
    <move file="${output.dir}" tofile="${output.dir}_1"/>
    <property name="newdirectory" value="${dest}"/>
</move.exists/>

<target name="move.does.not.exists"
    unless="output.dir.exists"/>
    <echo message="Directory does not exist" />
</target>

すべてをエコーし​​なかった場合、構造は少しきれいになります。

<target name="move.test">
    <available file="${output.dir}" type="dir"
        property="output.dir.exists"/>
</target>

<target name="move"
    depends="move.test, backup">
    <move file="${newdirectory}" todir="C:\reports" />
</target>

<target name="backup"
    if="output.dir.exists">
    <move file="${output.dir}" tofile="${output.dir}_1"/>
    <property name="newdirectory" value="${dest}"/>
</move.exists/>
于 2013-08-21T12:17:36.173 に答える
0

if/else コンストラクトは、すぐに使用できる Ant のネイティブ部分ではなく、ant-contribプロジェクトによって提供されます。JAR をダウンロードして、関連する<taskdef>ものをビルド ファイルに追加する必要があります。たとえば、ant-contrib-1.0b3.jarbuildをダウンロードして、build.xml と同じディレクトリ内の名前の付いたディレクトリに配置する場合、次のように言うことができます。

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="build/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>
于 2013-08-21T11:35:11.360 に答える