Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Package does not exist

I'm trying to build my project and I keep getting an error, saying the package doesn't exist why the IDE says it's fine (no compile error). Obviously, something is not consistent between my pom.xml and the way the IDE compiles.

Here how my project structure looks like: enter image description here

Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.oo</groupId>
    <artifactId>employeeservice</artifactId> <!-- Docker complains if the name contains upper case -->
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>EmployeeService</name>
    <description>This service takes care of all the employee related operations</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

If I remove Employee (entity) and EmployeeRepository and just have the controller with a dummy "Hello World", it works fine! I tried to put them under the same package, same issue!

Employee class

package com.oo.employeeservice.dao.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String firtName;

    private String lastName;

    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirtName() {
        return firtName;
    }

    public void setFirtName(String firtName) {
        this.firtName = firtName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

Using default Spring boot configuration:

package com.oo.employeeservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

}

Controller

package com.oo.employeeservice.controller;

import com.oo.employeeservice.dao.EmployeeRepository;
import com.oo.employeeservice.dao.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class EmployeeController {

    @Autowired
    private EmployeeRepository repo;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Iterable<Employee>> all() {
        return new ResponseEntity<Iterable<Employee>>(repo.findAll(), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> create(@RequestBody Employee employee) {
        repo.save(employee);
        return new ResponseEntity<String>(HttpStatus.CREATED);
    }

}

Maven log:

~/IdeaProjects/EmployeeService$ mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building EmployeeService 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ employeeservice ---
[INFO] Deleting /home/mahdi/IdeaProjects/EmployeeService/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ employeeservice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ employeeservice ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/mahdi/IdeaProjects/EmployeeService/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[3,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[9,60] cannot find symbol
  symbol: class Employee
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[4,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[18,36] cannot find symbol
  symbol:   class Employee
  location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[23,50] cannot find symbol
  symbol:   class Employee
  location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[19,44] cannot find symbol
  symbol:   class Employee
  location: class com.oo.employeeservice.controller.EmployeeController
[INFO] 6 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.872 s
[INFO] Finished at: 2018-07-15T15:59:38+08:00
[INFO] Final Memory: 31M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project employeeservice: Compilation failure: Compilation failure: 
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[3,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[9,60] cannot find symbol
[ERROR]   symbol: class Employee
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[4,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[18,36] cannot find symbol
[ERROR]   symbol:   class Employee
[ERROR]   location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[23,50] cannot find symbol
[ERROR]   symbol:   class Employee
[ERROR]   location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[19,44] cannot find symbol
[ERROR]   symbol:   class Employee
[ERROR]   location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
like image 602
Max ZooZoo Avatar asked Jul 15 '18 07:07

Max ZooZoo


1 Answers

I found the silliest problem with Intellij. If you look for Employee class in the package explorer in the image attached, you can see the file is marked as a class. When I opened the file location in the terminal, I realized it doesn't have ".java" extension. Obviously, maven won't recognize the file as a java file!

like image 200
Max ZooZoo Avatar answered Oct 04 '22 08:10

Max ZooZoo