2

に があり<path id="...">ますbuild.xml。コンパイラを起動する前に、クラスパス上のすべての jar/ディレクトリが存在することを確認し、不足しているものについて警告を出力したいと考えています。誰かが既存のソリューションを知っていますか、それともそのために自分のタスクを書く必要がありますか?


OK、カスタム タスクを実行することにしました。誰かがそのようなものを必要とする場合に備えて、ここにあります:

import java.io.File;
import java.util.Iterator;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.Resources;

public class CheckClasspathTask extends Task {

    private Reference reference;

    public CheckClasspathTask() {
    }

    public void setRefId(Reference reference) {
        this.reference = reference;
    }

    public void execute() throws BuildException {
        Resources resources = new Resources();
        resources.setProject(getProject());
        resources.add((ResourceCollection) reference.getReferencedObject());
        boolean isFirst = true;
        for (Iterator i = resources.iterator(); i.hasNext(); ) {
            String f = i.next().toString();
            if (!new File(f).exists()) {
                if (isFirst) {
                    isFirst = false;
                    System.out.println("WARNING: The following entries on your classpath do not exist:");
                }
                System.out.println(f);
            }
        }
    }
}
4

3 に答える 3

2

これにより、クラスパス内のすべてのファイルまたはフォルダーが存在することが確認されます。それらのいずれかがそうでない場合、ビルドは失敗し、見つからない最初の名前が表示されます。

<target name="check-classpath" depends="create-classpath">
    <pathconvert pathsep="," property="myclasspath" refid="compile.classpath"/>
    <foreach list="${myclasspath}" param="file" target="check-file-or-folder-exists" />
</target>

<target name="check-file-or-folder-exists">
    <fail message="Error: ${file} not found">
      <condition>
        <not>
          <available file="${file}" />
        </not>
      </condition>
    </fail>
</target>

<foreach>ant-contribs にあることに注意してください-- Ant Contribs のクリック可能なリンク

これは、ant-contrib jar をロードし、その中のすべてのターゲットをフックするために必要になります。

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${some.lib.dir}/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<for>すべてのパス要素を続行し、最後にエラーのみを表示するオプションを設定できるタスクが存在するはずです。私のバージョンの Eclipse が<foreach>既にクラスパスにあることがわかったので、これで十分だと思いました。

于 2013-07-26T20:09:12.433 に答える
1

おそらく、このbuild.xmlの ' ' ターゲットでorg.netbeans.modules.bpel.project.anttasks.ValidateBPELProjectTask行われたのと少し似た、カスタム ant タスクが適切であると言えます。pre-dist

注: ValidateBPELProjectTask Ant タスクは、通常のカスタム タスクよりも少し複雑です。実行するには独自のクラスパスが必要です (クラスパスは最初に build.xml に渡されません)。
現在の Ant タスク クラスローダーのクラスパスを変更できないため、ValidateBPELProjectTask は new を定義し、AntClassLoaderを呼び出しますsetContextClassLoader()

ただし、そのようなメカニズムは必要ありません。チェックするディレクトリのリストをパラメーターとしてタスクに渡すだけです。

于 2009-04-15T16:47:48.717 に答える
1

デフォルト タスクを使用して Ant スクリプト内のパスを反復処理する方法はあまり見つかりませんでした。UNIX ライクなシェルを持つマシンでビルドしている場合は、シェルを呼び出してクラスパス要素を確認できます。

シェル スクリプトを呼び出すときはapplytaskを使用できますが、クラスパス要素が存在しない場合は出力できませんでした。

次のクラスパス宣言があるとします。

<path id="your.classpath">
    <fileset dir="your.libs"/>
    <pathelement location="/some/missing/dir"/>
</path>

これにより、欠落している要素があるかどうかが報告されますが、どれが欠落しているかはわかりません。

<apply executable="test" type="file" ignoremissing="false">
    <arg value="-e"/>
    <srcfile/>
    <path refid="build.classpath"/>
</apply>

これを単純なシェルスクリプトと組み合わせて、executable属性を変更することで必要なものを取得できます-ビルドの失敗ではなく、大きな警告メッセージが必要な場合:

#!/bin/sh

test -e "$1" || echo "WARNING: Classpath element $1 does not exist!"

ビルドを失敗させたいapply場合は、エラーが報告された場合 (この場合はファイル/ディレクトリが見つからない場合) にタスクを失敗するように設定し、警告が出力された後にゼロ以外の終了コードを返すようにシェル スクリプトを変更できます。

exec別の方法は、タスクを使用して、インラインで小さなスクリプトを実行することです。

<pathconvert property="classpath" refid="build.classpath" pathsep=":"/>
<exec executable="sh">
    <arg value="-c"/>
    <arg value="for f in `echo ${classpath} | tr ':' '\n'`; do test -e $f || echo WARNING: Classpath element $f     does not exist!; done"/>
</exec>
于 2009-04-15T18:07:09.273 に答える