【问题标题】:In maven Project testng.xml runs successfully but pom.xml is giving Compilation error在 maven 项目中 testng.xml 运行成功,但 pom.xml 给出了编译错误
【发布时间】:2021-06-29 20:08:49
【问题描述】:

我一直在尝试解决问题,但无法解决。您想分享您对此的看法吗? 我确实添加了 maven-surefire-plugin 插件,但它不起作用,并且由于 pom.xml 我构建的 jenkins 也失败了。

在下面分享我的 pom.xml 和 testng.xml。

  1. testng.xml
 *<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test thread-count="5" name="Test">
        <classes>
          <class name="com.pages.VisibilityAnnotationDemo"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->*

2)Pom.xml

    *<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>VRsessions</groupId>
    <artifactId>VRsessions</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>VRsessions</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>

        <testSourceDirectory>src/main/java/com/pages/VisibilityAnnotationDemo</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
                <configuration>
                    <testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <includes>
                        <include>VisibilityAnnotationDemo.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        


    </dependencies>
</project>

错误日志如下

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project VRsessions: Compilation failure: Compilation failure. 
package org.testng does not exist
cannot find symbol
[ERROR] symbol:   class Test
[ERROR] location: class com.pages.VisibilityAnnotationDemo

【问题讨论】:

  • 请添加编译错误信息。可能这提供了仅此代码 sn-ps 的更多信息。还可以尝试修复您帖子的布局问题,以更清楚地了解您的 sn-ps 中的真正内容。
  • 你在添加testng依赖后做了Maven-&gt; Update project吗?
  • @GauthamM 是的,我做到了。
  • @Tejas 是测试源目录&lt;testSourceDirectory&gt;src/main/java/com/pages/VisibilityAnnotationDemo&lt;/testSourceDirectory&gt; 正确吗?不应该是test 而不是main 吗? src/test/java..............
  • @GauthamM...我总是在 src/main 中创建 java 类。它应该在 src/test 文件夹中吗?

标签: java xml maven testng


【解决方案1】:

要解决这个问题,请将依赖范围从测试更改为编译:

 <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.14.3</version>
      <scope>test</scope>
 </dependency>

  <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>compile</scope>
    </dependency>

1) 编译 这是默认范围,如果未指定则使用。编译依赖项在项目的所有类路径中都可用。此外,这些依赖项会传播到相关项目。

注意:默认范围是编译

2) 测试 这个范围表示该依赖不是应用程序正常使用所必需的,仅适用于测试编译和执行阶段。这个范围是不可传递的。

注意:测试范围只允许在测试阶段使用依赖项。

截图&lt;scope&gt;test&lt;/scope&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多