Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

[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 -version I get this:

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:

<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...)
like image 829
kroe761 Avatar asked Apr 05 '17 19:04

kroe761


People also ask

How do I fix Maven dependency issues?

If the dependencies weren't imported correctly (IntelliJ IDEA highlights them), try to perform the following actions: You can check your local maven repository in the Maven | Repositories settings and try to update it. You can check the jar file of the local . m2 repository to see if it was downloaded correctly.

What is groupId in pom xml?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.

What is groupId in Maven?

groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org. apache. maven.


2 Answers

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

src/main/java

Test sources - which are located:

src/test/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

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

Maven has his own lifecycle.

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

And during compile you 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 test phase.
Thus compilation fails.

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.

like image 92
catch23 Avatar answered Oct 23 '22 01:10

catch23


You need to run maven clean install command first.

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

UPDATE: You should change your com.company.automation.ecom.pages package to your src/main/java folder. so that it can be compiled in maven compile phase.

like image 25
alayor Avatar answered Oct 22 '22 23:10

alayor