Running ANT Programmatically Through Java

2019-01-20 07:28发布

问题:

I've a requirement where I have to extract metrics from different Java projects (configured in Eclipse) through Metrics Plugin. I am trying to automate this process using ANT build scripts.

I've made a Java utility project that will:

  1. Create these build files
  2. Put them in respective Java projects configured in Eclipse
  3. Attempt to execute them.

While the build.xml are running correctly, when I try to run them programmatically, I get the following error:

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)

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"

The build file is as follows:

<project basedir="." default="init" name="projectName">
    <target name="init">
        <tstamp/>
    </target>

    <target depends="init" name="build">
        <eclipse.refreshLocal depth="infinite" resource="projectName"/>
        <metrics.enable projectName="projectName"/>
        <eclipse.build BuildType="full" ProjectName="projectName" errorFormat="xml" errorOut="errors.xml" failOnError="true"/>
        <metrics.export file="outputFile" projectName="projectName"/>
        </target>
</project>

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:

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 < 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");
        }
    }
}

回答1:

One of the issues is probably the way you're getting a ProjectHelper instance. Try:

ProjectHelper helper = ProjectHelper.getProjectHelper();

It also doesn't seem to make sense that you're calling Project.init() after adding a reference and setting a property. It might be cleaner to just create a new project for each build file, and call init before calling addReference and setProperty.



标签: java ant metrics