下面是我的 Build.xml 文件。我试图通过 Build.xml 执行 TestNG.xml,但每次都会收到以下错误。
[testng] 在类路径中找不到类:
下面的文件在编译目标之前工作正常。我猜想 exec 目标中编写的代码存在一些问题。
如果我尝试单独执行 TestNG.xml 文件,它工作得很好。没有问题。但当我从 Build.xml 执行 TestNG.xml 时,问题就出现了。
我尝试通过在 stackoverflow 上找到的多种方法来解决该问题,例如:
- 清理项目
- 更新 TestNG 库 但没有成功。
有人可以帮我解决这个问题吗?
<property name="lib" value="./lib" />
<property name="bin" value="./bin" />
<path id="Sample Ant build.classpath">
<pathelement location="${output}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="init">
<delete dir="${bin}">
</delete>
<mkdir dir="${bin}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}"
destdir="${bin}"
includeantruntime="false"
debug="true">
<classpath refid="Sample Ant build.classpath"/>
</javac>
</target>
<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="Sample Ant build.classpath" />
<target name="exec" depends="compile">
<mkdir dir="${testng_report_dir}" />
<testng outputdir="${testng_report_dir}" classpathref="Sample Ant build.classpath" useDefaultListeners="true">
<xmlfileset dir="${basedir}" includes="Testng.xml" />
</testng>
</target>
最佳答案
问题出在我在 build.xml 中设置的类路径中。下面是正确的类路径。
<path id="classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
${bin} 是编译 .java 文件后 .class 文件所在的目录。
关于java - 从 Build.xml(ANT) 执行 TestNG.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573349/