java - 从 Build.xml(ANT) 执行 TestNG.xml 文件

标签 java xml apache ant testng

下面是我的 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/

相关文章:

Java 性能历史

java - 什么是 Maven 存储库协议(protocol)?

xml - R WebCrawler - XML 内容似乎不是 XML :

java - 使用 DrawerLayout 在 fragment 中设置工具栏标题

Java 评估字符串作为表达式或代码执行

java - 特征脸的相关矩阵 - Java

java - 从 jsf 组件调用 javax.el.MethodExpression

c# - 在 C# 中将 Xml 元素复制到另一个文档

linux - 无法删除自签名证书以在 Virtualmin 上使用我自己的证书

apache - htaccess 将 .php 添加到没有扩展名的所有内容中