title: Running ANT Programmatically Through Java
body: <p>I've a requirement where I have to extract metrics from different Java projects (configured in Eclipse) through <a href="http://metrics.sourceforge.net/" rel="nofollow noreferrer">Metrics Plugin</a>. I am trying to automate this process using ANT build scripts.</p>
  
  <p>I've made a Java utility project that will:</p>
  
  <ol>
  <li>Create these build files</li>
  <li>Put them in respective Java projects configured in Eclipse</li>
  <li>Attempt to execute them.</li>
  </ol>
  
  <p>While the build.xml are running correctly, when I try to run them programmatically, I get the following error:</p>
  
  <pre><code>Exception in thread "main" ProjectHelper.parse() must be implemented in a helper plugin org.apache.tools.ant.ProjectHelper
  at org.apache.tools.ant.ProjectHelper.parse(ProjectHelper.java:277)
  at com.metrics.generator.MetricsGenerator.runAntScripts(MetricsGenerator.java:69)
  at com.metrics.generator.MetricsGenerator.doAutomate(MetricsGenerator.java:43)
  at com.metrics.generator.MetricsGenerator.main(MetricsGenerator.java:30)
  </code></pre>
  
  <p>I am assuming that the parse() method is not working correctly, but I don't understand why. Running the build.xml manually works (which means that the XML file is correctly created). The only modification that I had to make for Eclipse to run this build was to go to Run > External Tools > External Tools Configuration > JRE and select "Run in the same JRE as the workspace"</p>
  
  <p>The build file is as follows:</p>
  
  <pre><code>&lt;project basedir="." default="init" name="projectName"&gt;
      &lt;target name="init"&gt;
          &lt;tstamp/&gt;
      &lt;/target&gt;
  
      &lt;target depends="init" name="build"&gt;
          &lt;eclipse.refreshLocal depth="infinite" resource="projectName"/&gt;
          &lt;metrics.enable projectName="projectName"/&gt;
          &lt;eclipse.build BuildType="full" ProjectName="projectName" errorFormat="xml" errorOut="errors.xml" failOnError="true"/&gt;
          &lt;metrics.export file="outputFile" projectName="projectName"/&gt;
          &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Note: [projectName] and [ouputFile] are currently provided at the time of their creation, I just omitted them from this post. FYI, I'm using Eclipse Indigo.
  The code where it fails looks as follows:</p>
  
  <pre><code>private void runAntScripts(File[] projectFolders){
      BuildLogger logger = new DefaultLogger();
      logger.setOutputPrintStream(System.out);
      logger.setErrorPrintStream(System.out);
      logger.setMessageOutputLevel(Project.MSG_INFO);
  
      Project metricsProject  = new Project();
      metricsProject.addBuildListener(logger);
  
      ProjectHelper helper = new ProjectHelper();
      metricsProject.addReference("ant.projectHelper", helper);
  
      File buildFile;
  
      int totalFolders = projectFolders.length;
      for(int index = 0; index &lt; totalFolders; index++){
  
          buildFile = new File(projectFolders[index], buildXMLFileName);
  
          if(buildFile.isFile()){
              helper.parse(metricsProject, buildFile);
              metricsProject.setProperty("ant.file", buildFile.getAbsolutePath());
  
              metricsProject.init();
              metricsProject.setBaseDir(projectFolders[index]);
              metricsProject.executeTarget("build");
          }
      }
  }
  </code></pre>