gpt4 book ai didi

java - Maven: 'Package does not exist'(和其他错误)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:48:31 26 4
gpt4 key购买 nike

我正在使用 Maven 运行多个 selenium 自动化测试。当我在 Eclipse 中调试时,我通常只需右键单击 testing.xml 并选择 Run As > TestNG Suite。但是在Jenkins中运行需要使用mvn test来运行。但是当我运行它时,出现了几个错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project ecom: Compilation failure: Compilation failure:
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[15,43] package com.company.automation.ecom.pages does not exist
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[16,43] package com.company.automation.ecom.pages does not exist
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[110,13] cannot find symbol
[ERROR] symbol: class Header
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[110,62] cannot find symbol
[ERROR] symbol: class Header
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[113,9] cannot find symbol
[ERROR] symbol: class SignIn
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[113,58] cannot find symbol
[ERROR] symbol: class SignIn
[ERROR] location: class com.company.automation.ecom.HelperMethods

我知道文件存在,当我作为 TestNG 套件运行时,一切正常。此外,当我运行 which java -version 我得到这个:

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

我知道这是某个地方的配置问题,但我对 maven/java 配置的了解还不够,无法弄清楚。 Maven 告诉我的文件已经消失了是我的文件,而且它们绝对存在。这是我的 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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>com.kirklands.automation.ecom.retry.MyTestListenerAdapter</value>
</property>
</properties>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<groupId>com.kirklands.automation</groupId>
<artifactId>ecom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ecom</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

项目结构:

src/main/java
{package com.company.automation.ecom}
CreditCard.java
HelperMethods.java
src/test/java
{package com.company.automation.ecom.pages}
Header.java
SignIn.Java
(etc...)
{package com.company.automation.ecom.tests}
HeaderTests.java
(etc...)

最佳答案

您的问题似乎是由于您尝试从您的访问:

src/main/java

测试源 - 位于:

src/test/java

这是您发布的日志的确切片段:

/Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[15,43] package com.company.automation.ecom.pages does not exist

它会从另一个方面起作用:如果您将从测试范围 (src/test/java) 使用源代码 (src/main/java)。

Maven 有自己的 lifecycle .

它有一个严格的后果:

  • compile - 编译项目的源代码
  • test - 使用合适的单元测试框架测试编译后的源代码。这些测试不应要求打包或部署代码

并且在编译期间,您只能编译您的源代码。但是,这取决于你的测试(pages包),暂时不能编译,因为它只会在test阶段编译。
因此编译失败

为了解决这个问题,尝试改变你的项目结构,一点点:

src/main/java
{package com.company.automation.ecom}
CreditCard.java
HelperMethods.java
{package com.company.automation.ecom.pages}
Header.java
SignIn.Java
(etc...)
src/test/java
{package com.company.automation.ecom.tests}
HeaderTests.java

并且您的测试应该毫无问题地使用源(核心和页面)。

关于java - Maven: 'Package does not exist'(和其他错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43240059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com