运行运行目标时出现Ant构建错误

时间:2014-07-06 22:22:13

标签: java xml ant

我正在使用ant 1.9.0来运行我的Java项目。我有一个bat文件,它有命令ant clean compile run.the clean和compile正在运行但是对于目标运行我得到以下错误

 C:\Users\Susanta\MyMaven\MyMavenProject\build] on class org.apache.
 tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter: java.lang.NoClassDefFoundErrorjunit/framework/TestListener

我正在附加build.xml,请告诉我build.xml中的问题

<?xml version="1.0" encoding="iso-8859-1"?>
  <!DOCTYPE project [
]>

  <project name="MyMavenProject" default="usage" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>

<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="${user.home}/.m2/repository/"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="test.reportsDir" value="${test.dest}/test_reports"/>




<path id="testcase.path">


    <pathelement location="${test.dest}"/>

    <fileset dir="${ws.jars}">
      <include name="*.jar"/>
    </fileset>

  </path>




<target name="setClassPath" unless="test.classpath">
    <path id="classpath_jars">
        <fileset dir="${ws.jars}" includes="**/*.jar"/>
    </path>
    <pathconvert pathsep=":" 
        property="test.classpath" 
        refid="classpath_jars"/>
</target>

<target name="init" depends="setClassPath">
    <tstamp>
        <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
    </tstamp>
    <condition property="ANT" 
        value="${env.ANT_HOME}/bin/ant.bat" 
        else="${env.ANT_HOME}/bin/ant">
                <os family="windows" />
    </condition>

</target>

<!-- all -->
<target name="all">
</target>

<!-- clean -->
<target name="clean">
    <delete dir="${test.dest}"/>
</target>

<!-- compile -->
<target name="compile" depends="init, clean" > 
    <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.dest}" includes="**/*"/>
    </delete>
    <echo message="making directory..."/>
    <mkdir dir="${test.dest}"/>
    <echo message="classpath------: ${test.classpath}"/>
    <echo message="compiling..."/>
    <javac 
        debug="true" 
        destdir="${test.dest}" 
        srcdir="${test.src}" 
        target="1.5" 
        classpath="${test.classpath}"
    >
    </javac>
  </target>

<!-- build -->
<target name="build" depends="init">
</target>




<target name="usage">
    <echo>
        ant run will execute the test
    </echo>
</target>

<path id="test.run">
        <fileset dir="${ws.jars}" includes="**/*.jar"/>
</path>



<target name="run" >  
    <delete includeemptydirs="true" quiet="true">
                <fileset dir="${test.reportsDir}" includes="**/*"/>
            </delete>
            <java jar="${ws.jars}" fork="true" spawn="true" />  
            <junit fork="yes" haltonfailure="no" printsummary="yes">
                <classpath refid="test.run" />  

              <!--  <classpath ="${test.classpath}"/> -->  
            <batchtest  todir="${test.reportsDir}" fork="true">  
            <fileset dir="${test.dest}">  

          <!--include name="tests/LoginTest.class" />
         <include name="tests/Parameterized_Test.class" />
         <include name="tests/TestApplication.class" /-->
          <include name="com/testing/GoogleTest.class" />
          <include name="com/testing/YahooTest.class" />





            </fileset>  
            </batchtest>    
                <formatter type="xml" />  
                <classpath refid="testcase.path" />  
        </junit>  
            <junitreport todir="${test.reportsDir}">    
                <fileset dir="${test.reportsDir}">  
                    <include name="TEST-*.xml" />  
                </fileset>  
                <report todir="${test.reportsDir}" />  
            </junitreport>  
        </target>

以下是错误:

   C:\Users\Susanta\MyMaven\MyMavenProject\build] on class org.apache.
  tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter: java.lang.NoClassDefF
 oundError: junit/framework/TestListener
 [exec]     at java.lang.ClassLoader.defineClass1(Native Method)
 [exec]     at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
 [exec]     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
 [exec]     at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
 [exec]     at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
 [exec]     at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
 [exec]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 [exec]     at java.security.AccessController.doPrivileged(Native Method)
 [exec]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 [exec]     at org.apache.tools.ant.AntClassLoader.findBaseClass(AntClassLoader.java:1385)
 [exec]     at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoaderjava:1064)
 [exec]     at org.apache.tools.ant.util.SplitClassLoader.loadClass(SplitClassLoader.java:58)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 [exec]     at java.lang.ClassLoader.defineClass1(Native Method)
 [exec]     at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
 [exec]     at org.apache.tools.ant.AntClassLoader.defineClassFromData(AntClassLoader.java:1128)
 [exec]     at org.apache.tools.ant.AntClassLoader.getClassFromStream(AntClassLoader.java:1299)
 [exec]     at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1354)
 [exec]     at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoaderjava:1315)
 [exec]     at org.apache.tools.ant.util.SplitClassLoader.loadClass(SplitClassLoader.java:52)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 [exec]     at java.lang.Class.forName0(Native Method)
 [exec]     at java.lang.Class.forName(Class.java:266)
 [exec]     at 

   org.apache.tools.ant.taskdefs.optional.junit.FormatterElement.createFormatter(FormatterElement.java:286)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.logVmExit(JUnitTask.java:1653)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.logVmCrash(JUnitTask.java:1617)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1099)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:851)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899)
 [exec]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800)
 [exec]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
 [exec]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
 [exec]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [exec]     at java.lang.reflect.Method.invoke(Method.java:601)
 [exec]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
 [exec]     at org.apache.tools.ant.Task.perform(Task.java:348)
 [exec]     at org.apache.tools.ant.Target.execute(Target.java:435)
 [exec]     at org.apache.tools.ant.Target.performTasks(Target.java:456)
 [exec]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
 [exec]     at org.apache.tools.ant.Project.executeTarget(Project.java:1364)

 [exec]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
 [exec]     at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
 [exec]     at org.apache.tools.ant.Main.runBuild(Main.java:851)
 [exec]     at org.apache.tools.ant.Main.startAnt(Main.java:235)
 [exec]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
 [exec]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
 [exec] Caused by: java.lang.ClassNotFoundException: junit.framework.TestListener
 [exec]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 [exec]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 [exec]     at java.security.AccessController.doPrivileged(Native Method)
 [exec]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 [exec]     ... 48 more
 [exec]
 [exec] Total time: 7 seconds
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.773s
[INFO] Finished at: Mon Jul 07 03:01:39 IST 2014
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:
run (default) on project MyMavenProject: An Ant BuildException has occured: exec
 returned: 1
[ERROR] around Ant part ...<exec dir="C:\Users\Susanta\MyMaven\MyMavenProject" e
xecutable="C:\Users\Susanta\MyMaven\MyMavenProject\testrunner.bat" failonerror="
true">... @ 5:142 in C:\Users\Susanta\MyMaven\MyMavenProject\target\antrun\build
-runtheapp.xml
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal o
rg.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project MyMaven
Project: An Ant BuildException has occured: exec returned: 1
around Ant part ...<exec dir="C:\Users\Susanta\MyMaven\MyMavenProject" executabl
e="C:\Users\Susanta\MyMaven\MyMavenProject\testrunner.bat" failonerror="true">..
. @ 5:142 in C:\Users\Susanta\MyMaven\MyMavenProject\target\antrun\build-runthea
pp.xml
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:317)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    Caused by: org.apache.maven.plugin.MojoExecutionException: An Ant BuildException
  has occured: exec returned: 1
 around Ant part ...<exec dir="C:\Users\Susanta\MyMaven\MyMavenProject"    executable="C:\Users\Susanta\MyMaven\MyMavenProject\testrunner.bat" failonerror="true">..
. @ 5:142 in C:\Users\Susanta\MyMaven\MyMavenProject\target\antrun\build-runtheapp.xml

3 个答案:

答案 0 :(得分:1)

这似乎是ANT 1.9.0中的 已知错误 。见这里:Bamboo Ant Task fails when running junit task

阅读comments #4, #5, #8 & #20 @ THIS Link寻求帮助。

答案 1 :(得分:0)

ANT手册描述了junit task如何需要一个额外的罐子。这可以从Maven Central下载

并安装在以下目录中:

  • $ HOME /赵军阳张志利/ lib中

我个人使用一个名为ivy的ANT插件来管理我的构建依赖项,请参阅:

答案 2 :(得分:0)

查看您的错误消息:

C:\Users\Susanta\MyMaven\MyMavenProject\build] on class \ 
    org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter: \
    java.lang.NoClassDefFoundError junit/framework/TestListener

请注意NoClassDefFoundError,这意味着Ant无法找到您的某个任务所引用的类。是否有与此错误相关联的行号?应该有一个指向包含<junit/>任务的行。即使没有,您也会发现找不到的课程是junit/framework/TestListener

Java应用程序中的错误消息可能有点令人生畏,因为您会看到数十,甚至数百行的堆栈跟踪。但是,将Java程序崩溃视为车祸。一辆车可能已经在路上行驶,打了一根电线杆,从那里弹开,撞到另一辆车,从那里弹开,撞上了消防栓等等。你看到了很长的损坏痕迹,但如果你看第一辆发生事故的地方,你通常可以看到问题(看,路上有香蕉皮!)

Java错误消息也是如此。第一行通常会告诉您确切需要知道的内容,并且异常非常容易阅读。在这种情况下,NoClassDefFoundError并指向junit作为罪魁祸首。下次你看到这样的东西时,你会知道要找什么。


正如Mark O'Connor所指出的,JUnit任务所需的jar不包含在Ant中。实际上,很多已定义的任务都没有运行这些任务所需的jar,正如您在this table中看到的那样(<junit>是该表中的第四项)。

JUunit task的Ant手册中,它声明您可以执行以下操作:

  

注意:您必须拥有junit.jar。您可以执行以下操作之一:

     
      
  1. junit.jarant-junit.jar放入ANT_HOME/lib
  2.   
  3. 不要将它们放在ANT_HOME/lib中,而是将它们的位置包含在CLASSPATH环境变量中。
  4.   
  5. 使用-lib将两个JAR添加到类路径中。
  6.   
  7. 使用构建文件中<classpath>中的<taskdef>元素指定两个JAR的位置。
  8.   
  9. ant-junit.jar保留在ANT_HOME/lib的默认位置,但在传递到的地址中包含junit.jar。 (自Ant 1.7起)
  10.   

我个人更喜欢最后一个。我通常将junit.jar放在${basedir}/antlib/junit中。这样,当有人签出我的项目时,他们会得到junit

Ant FAQ中,它讨论了<junit>的一些问题。