运行运行目标时Ant生成错误



我使用ant 1.9.0来运行我的Java项目。我有一个bat文件,其中有命令ant clean compile运行。clean and compile正在运行,但对于目标运行,我得到的错误

 C:UsersSusantaMyMavenMyMavenProjectbuild] 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:UsersSusantaMyMavenMyMavenProjectbuild] 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:UsersSusantaMyMavenMyMavenProject" e
xecutable="C:UsersSusantaMyMavenMyMavenProjecttestrunner.bat" failonerror="
true">... @ 5:142 in C:UsersSusantaMyMavenMyMavenProjecttargetantrunbuild
-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:UsersSusantaMyMavenMyMavenProject" executabl
e="C:UsersSusantaMyMavenMyMavenProjecttestrunner.bat" failonerror="true">..
. @ 5:142 in C:UsersSusantaMyMavenMyMavenProjecttargetantrunbuild-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:UsersSusantaMyMavenMyMavenProject"    executable="C:UsersSusantaMyMavenMyMavenProjecttestrunner.bat" failonerror="true">..
. @ 5:142 in C:UsersSusantaMyMavenMyMavenProjecttargetantrunbuild-runtheapp.xml

这似乎是ANT 1.9.0中的已知错误。请参阅此处:运行junit任务时,Bamboo Ant任务失败

阅读comments #4, #5, #8 & #20@THIS链接以获得一些帮助。

ANT手册描述了junit任务如何需要额外的jar。这可以从Maven Central 下载

  • junit版本列表

并安装在以下目录中:

  • $HOME/.ant/lib

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

  • 在build.xml中找不到包含Ant、Ivy和JUnit的类-错误
  • 在netbeans中使用Ant在构建期间动态获取外部库的最新版本

查看您的错误消息:

C:UsersSusantaMyMavenMyMavenProjectbuild] 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(<junit>是该表中的第四项(。

在关于JUunit任务的Ant手册中,它指出您可以执行以下操作:

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

  1. junit.jarant-junit.jar都放在ANT_HOME/lib
  2. 不要将两者都放在ANT_HOME/lib中,而是将它们的位置包含在CLASSPATH环境变量中
  3. 使用-lib将两个JAR添加到类路径中
  4. 使用生成文件中<taskdef>中的<classpath>元素指定两个JAR的位置
  5. ant-junit.jar保留在ANT_HOME/lib中的默认位置,但将junit.jar包括在传递给的中。(自Ant 1.7以来(

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

在Ant常见问题解答中,它讨论了<junit>的一些问题。

相关内容

  • 没有找到相关文章

最新更新