我正在尝试构建我的项目,但不断收到错误消息,说该软件包不存在,因此IDE表示可以(没有编译错误)。显然,我的pom.xml与IDE的编译方式之间不一致。
这是我的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>
如果我删除了Employee(实体)和EmployeeRepository,而让控制器带有一个虚拟的“ Hello World”,那就可以了!我试图将它们放在相同的包装下,相同的问题!
员工类别
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;
}
}
使用默认的Spring引导配置:
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);
}
}
控制器
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日志:
~/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
答案 0 :(得分:0)
我发现Intellij最可怕的问题。如果在所附图像的包资源管理器中查找Employee类,则可以看到该文件被标记为类。当我在终端中打开文件位置时,我意识到它没有“ .java”扩展名。显然,maven不会将该文件识别为Java文件!
答案 1 :(得分:0)
我曾经遇到过同样的问题。问题是我已经创建了名称为 test.demo.exmple.java 的文件夹结构,并且在资源管理器中以相同的方式创建了文件夹(如果在资源管理器中打开相同的文件,则可以看到),这意味着文件夹名称为 test.demo ,系统在其中查找test / demo / example.java。
因此,在您的情况下,我认为路径将是 com.oo.employeeservice 而不是 com / oo / employeeservice