title: Programmatically executing ANT script in Java
body: <p>I've built a swing gui in eclipse that is supposed to run a bunch of code previously developed, part of which involves running ant to build the code.  When I run the any script outside of the GUI (in the original project) the ant executes correctly and builds the project.  However when I try and run ant programmatically it throws errors that look like the project doesn't have the necessary .jars.  The code, top of the build.xml, and errors are listed below. </p>
  
  <p>Code Block</p>
  
  <pre><code>File buildFile = new File("lib\\ePlay\\build.xml");
      Project p = new Project();
      p.setUserProperty("ant.file", buildFile.getAbsolutePath());
      DefaultLogger consoleLogger = new DefaultLogger();
      consoleLogger.setErrorPrintStream(System.err);
      consoleLogger.setOutputPrintStream(System.out);
      consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
      p.addBuildListener(consoleLogger);
  
      try {
          p.fireBuildStarted();
          p.init();
          ProjectHelper helper = ProjectHelper.getProjectHelper();
          p.addReference("ant.projectHelper", helper);
          helper.parse(p, buildFile);
          p.executeTarget(p.getDefaultTarget());
          p.fireBuildFinished(null);
      } catch (BuildException e) {
          p.fireBuildFinished(e);
      }
  </code></pre>
  
  <p>Build.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project name="EPlay" xmlns:ivy="antlib:org.apache.ivy.ant" default="resolve"&gt;
  
  &lt;dirname file="${ant.file}" property="ant.dir" /&gt;
  &lt;property name="scm.user" value="scmrel"/&gt;
  &lt;property name="scm.user.key" value="/.ssh/scmrel/id_rsa"/&gt;
  &lt;property name="ivy.jar" value="ivy-2.0.0.jar" /&gt;
  &lt;property name="ivy.settings.dir" value="${ant.dir}/ivysettings" /&gt;
  &lt;property name="VERSION" value="LATEST" /&gt;
  &lt;property name="tasks.dir" value="${ant.dir}/.tasks" /&gt;
  &lt;property name="deploy.dir" value="${ant.dir}/deploy" /&gt;
  ...
  &lt;!-- retrieve the dependencies using Ivy --&gt;
  &lt;target name="resolve" depends="_loadantcontrib,_getivy" description=" retrieve the dependencies with Ivy"&gt;
    &lt;ivy:settings file="${ivy.settings.dir}/ivysettings.xml" /&gt;
    &lt;ivy:resolve file="${ant.dir}/ivy.xml" transitive="false" /&gt;
    &lt;ivy:retrieve pattern="${deploy.dir}/[conf]/[artifact].[ext]"/&gt;
  &lt;/target&gt;
  </code></pre>
  
  <p>And the error</p>
  
  <pre><code>resolve:
  BUILD FAILED
  H:\eclipse\CLDeploySwing\lib\ePlay\build.xml:66: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings
  Cause: The name is undefined.
  Action: Check the spelling.
  Action: Check that any custom tasks/types have been declared.
  Action: Check that any &lt;presetdef&gt;/&lt;macrodef&gt; declarations have taken place.
  No types or tasks have been defined in this namespace yet
  
  This appears to be an antlib declaration. 
  Action: Check that the implementing library exists in one of:
          -ANT_HOME\lib
          -the IDE Ant configuration dialogs
  
  
  Total time: 0 seconds
  </code></pre>
  
  <p>I've looked through my ant installation and it <em>appears</em> everything is there.  Like I said, the original project builds successfully if build.xml is run outside of this application.</p>