使用Ant运行JUnit测试的Build.xml配置

时间:2018-02-22 04:13:11

标签: java xml junit ant

我尝试使用JAR从命令行构建BUILD FAILED MyApp/build.xml:73: The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath 文件,但我一直收到以下错误消息:

. ├── bin | └── packageOne | └── packageTwo | └── packageThree ├── dist ├── docs | ├── [All Generated Java Docs] ├── lib | ├── junit-4.11.jar | └── org.hamcrest.core_1.3.jar ├── src | ├── [All Source Files In Their Package] ├── test | ├── [All JUnit Tests in One Package] ├── testreport | ├── [Would like test report to be created here] ├── build.xml

目录结构如下:

build.xml

<?xml version="1.0"?> <project name="MyApp" default="main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="build.dir" location="bin" /> <property name="dist.dir" location="dist" /> <property name="docs.dir" location="docs" /> <property name="lib.dir" location="lib" /> <!-- Variables used for JUnit testing --> <property name="test.dir" location="test" /> <property name="test.report.dir" location="testreport" /> <!-- Deletes the existing build, docs and dist directory --> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${docs.dir}" /> <delete dir="${dist.dir}" /> <delete dir="${test.report.dir}" /> </target> <!-- Creates the build, docs and dist directory --> <target name="makedir"> <mkdir dir="${build.dir}" /> <mkdir dir="${docs.dir}" /> <mkdir dir="${dist.dir}" /> <mkdir dir="${test.report.dir}" /> </target> <!-- Create a classpath container which can be later used in the ant task --> <path id="build.classpath"> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> </path> <!-- Compiles the java code (including the usage of library for JUnit --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}"> <classpath refid="junit.class.path" /> </javac> </target> <!-- Creates Javadoc --> <target name="docs" depends="compile"> <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}"> <!-- Define which files / directory should get included, we include all --> <fileset dir="${src.dir}"> <include name="**" /> </fileset> </javadoc> </target> <!-- Define the classpath which includes the junit.jar and the classes after compiling --> <path id="junit.class.path"> <pathelement location="lib/junit-4.11.jar" /> <pathelement location="${build.dir}" /> </path> <!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain --> <target name="junit" depends="compile"> <junit printsummary="on" fork="true" haltonfailure="yes"> <classpath refid="junit.class.path" /> <formatter type="xml" /> <batchtest todir="${test.report.dir}"> <fileset dir="${src.dir}"> <include name="**/*Test*.java" /> </fileset> </batchtest> </junit> </target> <!-- Creates the deployable jar file --> <target name="jar" depends="compile"> <jar destfile="${dist.dir}\MyApp.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="MyApp.Main" /> </manifest> </jar> </target> <target name="main" depends="compile, docs, junit, jar"> <description>Main target</description> </target> </project> 的内容如下:

build.xml

我一直在关注Apache Ant tutorial,其中包含JUnit .jar的示例,但无论我将JUnit build.xml:64: The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.createMirror(JUnitTask.java:785) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.setupJUnitDelegate(JUnitTask.java:832) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:845) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292) at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:346) at org.apache.tools.ant.Target.execute(Target.java:448) at org.apache.tools.ant.Target.performTasks(Target.java:469) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399) at org.apache.tools.ant.Project.executeTarget(Project.java:1370) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:849) at org.apache.tools.ant.Main.startAnt(Main.java:228) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:283) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101) Caused by: java.lang.ClassNotFoundException: junit.framework.Test at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1385) at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1334) at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1089) at org.apache.tools.ant.util.SplitClassLoader.loadClass(SplitClassLoader.java:62) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.createMirror(JUnitTask.java:780) ... 18 more Total time: 1 second 文件放在何处,我仍会收到相同的错误。

此外,我发现了多个SO答案,表明我的build.xml存在问题,例如herehere

完整的详细错误消息是:

{{1}}

0 个答案:

没有答案