Java Maven:“包不存在”(和其他错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43240059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:48:04  来源:igfitidea点击:

Maven: 'Package does not exist' (and other errors)

javaeclipsemaven

提问by kroe761

I am running several selenium automated tests using Maven. When I'm debugging in Eclipse, I usually just right click on testing.xml and Run As > TestNG Suite. But running in Jenkins needs to be ran using mvn test. But when I run that, I get several errors:

我正在使用 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

I know the files are present, when I run as TestNG suite everything works with no issues. Additionally, when I run which java -versionI get this:

我知道文件存在,当我作为 TestNG 套件运行时,一切正常。此外,当我运行它时,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)

I know it's a configuration issue somewhere, but I don't know enough about maven/java configuration to figure it out. The files that Maven is telling me are gone are my files, and they are absolutely present. Here is my pom.xml file:

我知道这是某个地方的配置问题,但我对 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>

Project Structure:

项目结构:

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...)

采纳答案by nazar_art

Looks like your issue appears due to fact that you trying to access from your:

看起来您的问题是由于您尝试从以下位置访问而出现的:

src/main/java

源代码/主/Java

Test sources - which are located:

测试源 - 位于:

src/test/java

源代码/测试/Java

Here is the exact snippet of the log you posted:

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

/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

/Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[15,43] 包 com.company.automation.ecom.pages 不存在

It will work from another side: if you will use your sources (src/main/java) from test scope (src/test/java).

它将从另一边工作:如果您将使用src/main/java来自测试范围 ( src/test/java) 的源( )。

Maven has his own lifecycle.

Maven 有自己的生命周期

It has a strict consequence:

它有一个严格的后果

  • compile- compile the source code of the project
  • test- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • compile- 编译项目的源代码
  • test- 使用合适的单元测试框架测试编译的源代码。这些测试不应该要求打包或部署代码

And during compileyou can compile only your sources. However, it depends on your tests (pages package), which can't be compiled at this moment, because it will compile only at testphase.
Thus compilation fails.

在此期间,compile您只能编译您的源代码。但是,这取决于您的测试(页面包),此时无法编译,因为它只会在test阶段编译。
因此编译失败

For solving try to change your project structure, a little bit:

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

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

And your tests should use sources (core & pages) without any problem.

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

回答by alayor

You need to run maven clean installcommand first.

您需要先运行maven clean install命令。

That command will compile your missingpackage and it will even run the tests.

该命令将编译您的missing包,它甚至会运行测试。

UPDATE: You should change your com.company.automation.ecom.pagespackage to your src/main/javafolder. so that it can be compiled in maven compilephase.

更新:您应该将您的com.company.automation.ecom.pages包更改为您的src/main/java文件夹。以便它可以分阶段编译maven compile