在eclipse插件中调用ant

时间:2011-12-13 12:34:58

标签: eclipse ant junit eclipse-plugin jacoco

我有一个Eclipse插件,我需要在该插件中调用ant来制作一些东西,比如运行junit测试并获得该测试的覆盖范围(为此目的使用jacoco插件)...

我的 build.xml

<project name="Calculadora" default="rebuild" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
   <property name="src" location="./src"/>
   <property name="bin.dir" location="./bin"/>
   <property name="bin.report.dir" location="${bin.dir}/report"/>
   <property name="bin.exec.file" location="${bin.dir}/jacoco.exec"/>
   <property name="junit.out.dir" location="${bin.report.dir}/junit/xml"/>
   <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
      <classpath path="/tmp/jacocoant4002041518230825590.jar"/>
   </taskdef>
   <path id="classpath">
      <pathelement location="${bin.dir}"/>
      <pathelement location="${src}"/>
      <pathelement location="/usr/lib/eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar"/>
   </path>
   <target name="clean">
      <mkdir dir="${bin.report.dir}"/>
      <mkdir dir="${junit.out.dir}"/>
   </target>
   <target name="test" depends="clean">
      <jacoco:coverage destfile="${bin.exec.file}">
         <junit fork="true" forkmode="once">
            <classpath refid="classpath"/>
            <formatter type="xml" usefile="true"/>
            <test name="functions.test.ReadFile" todir="${junit.out.dir}"/>
         </junit>
      </jacoco:coverage>
   </target>
   <target name="report" depends="test">
      <jacoco:report>
         <executiondata>
            <file file="${bin.exec.file}"/>
         </executiondata>
         <structure name="">
            <classfiles>
               <fileset dir="${bin.dir}"/>
            </classfiles>
            <sourcefiles encoding="UTF-8">
               <fileset dir="${src}"/>
            </sourcefiles>
         </structure>
         <xml destfile="${bin.report.dir}/report.xml"/>
      </jacoco:report>
      <junitreport todir="${junit.out.dir}">
         <fileset dir="${junit.out.dir}">
            <include name="TEST-*.xml"/>
         </fileset>
      </junitreport>
   </target>
   <target name="rebuild" depends="clean,test,report"/>
</project>

在Java中调用Ant的代码:

// Creates a new Project object
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();

ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);

DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);

p.executeTarget(p.getDefaultTarget());

但是当我运行我的插件时,我收到了这个错误:

  

build.xml:34:java.lang.ClassNotFoundException:   org.apache.tools.ant.taskdefs.optional.junit.JUnitTaskMirrorImpl at   org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.createMirror(JUnitTask.java:708)   在   org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.setupJUnitDelegate(JUnitTask.java:745)   在   org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:755)   在   org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
  在org.jacoco.ant.CoverageTask.execute(CoverageTask.java:95)at   org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)   在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   在java.lang.reflect.Method.invoke(Method.java:616)at   org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)   在org.apache.tools.ant.Task.perform(Task.java:348)at   org.apache.tools.ant.Target.execute(Target.java:390)at   org.apache.tools.ant.Target.performTasks(Target.java:411)at at   org.apache.tools.ant.Project.executeSortedTargets(Project.java:1360)
  在org.apache.tools.ant.Project.executeTarget(Project.java:1329)   引起:java.lang.ClassNotFoundException:   org.apache.tools.ant.taskdefs.optional.junit.JUnitTaskMirrorImpl at   org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1386)   在   org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1336)   在   org.apache.tools.ant.util.SplitClassLoader.loadClass(SplitClassLoader.java:52)   在java.lang.ClassLoader.loadClass(ClassLoader.java:266)at   org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.createMirror(JUnitTask.java:701)   ......还有19个

发生了什么事?我做错了什么?

提前致谢

0 个答案:

没有答案