I have a <path id="...">
in my build.xml
. Before invoking the compiler I want to verify that every jar/directory on the classpath exists and print a warning with the missing ones. Does anybody know an existing solution or do I need to write my own task for that?
OK, I decided to go for a custom task. Here it is, in case anybody ever needs such a thing:
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);
}
}
}
}
I would say probably a custom ant task is in order, a little like the org.netbeans.modules.bpel.project.anttasks.ValidateBPELProjectTask
done in a 'pre-dist
' target of this build.xml.
Note: the ValidateBPELProjectTask ant task is a bit more complex than your usual custom task: it needs to have its own classpath to run (classpath not initially passed to the build.xml at first).
Since you cannot modify the classpath of the current ant task classloader, ValidateBPELProjectTask defines a new AntClassLoader
and calls setContextClassLoader()
.
You will not need such a mechanism though: you can just pass the list of directories to check to your task as a parameter.