title: Classpath validation in ant
body: <p>I have a <code>&lt;path id="..."&gt;</code> in my <code>build.xml</code>.  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?</p>
  
  <hr>
  
  <p>OK, I decided to go for a custom task.  Here it is, in case anybody ever needs such a thing:</p>
  
  <pre><code>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);
              }
          }
      }
  }
  </code></pre>