ant 使用java 运行类,运行ant编程通过Java

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:

- create these build files

- put them in respective java projects configured in eclipse

- 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:

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

}

}

}

Any help from you would be appreciated! Thanks!!

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值