我们有一个从头开始的多模块Spring boot项目,层次结构是这样的
hh(父母)
这是
account-service
.的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>
<parent>
<groupId>com.hh.sukku</groupId>
<artifactId>hh</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>account-service</artifactId>
<name>account-service</name>
<packaging>jar</packaging>
<description>account-service</description>
<dependencies>
<!-- Project module dependencies start -->
<dependency>
<groupId>com.hh.sukku</groupId>
<artifactId>common-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- Project module dependencies end -->
</dependencies>
</project>
应用程序服务 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>
<parent>
<groupId>com.hh.sukku</groupId>
<artifactId>hh</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>application-service</artifactId>
<packaging>war</packaging>
<name>application-service</name>
<description>application-service</description>
<dependencies>
<!-- Project module dependencies start -->
<dependency>
<groupId>com.hh.sukku</groupId>
<artifactId>common-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.hh.sukku</groupId>
<artifactId>account-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- Project module dependencies end -->
</dependencies>
</project>
这是父模块pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.hh.sukku</groupId>
<artifactId>hh</artifactId>
<version>1.0.0</version>
<name>hh</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<modules>
<module>common-service</module>
<module>account-service</module>
<module>application-service</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
问题出自
account-service
中的Controller,响应bean和API的错误码出自common-service
。最初在导入时它显示了两者的编译问题但是在做了maven-update
之后没有错误。
这是控制器
package com.hh.sukku.account.controller;
import static com.hh.sukku.common.util.ErrorCodes.SUCCESS;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hh.sukku.account.dto.UserDTO;
import com.hh.sukku.account.service.UserService;
import com.hh.sukku.common.beans.Response;
/**
*
* @author arun.sudhakaran
*
* 02-Apr-2023 11:14:15 pm
*/
@RestController
@RequestMapping("v1/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(path = "", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Response> create(@Valid @RequestBody UserDTO request) {
userService.create(request);
Response response = new Response(SUCCESS, "Success");
return new ResponseEntity<Response>(response, HttpStatus.OK);
}
}
当我们尝试在父模块上执行
maven clean install
时,问题就开始了,它失败并出现此错误
Maven 全新安装的控制台输出
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] hh [pom]
[INFO] common-service [jar]
[INFO] account-service [jar]
[INFO] application-service [war]
[INFO]
[INFO] --------------------------< com.hh.sukku:hh >---------------------------
[INFO] Building hh 1.0.0 [1/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hh ---
[INFO] Deleting D:\hh\haraji\workspace\hh\target
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ hh ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ hh ---
[INFO] Installing D:\hh\haraji\workspace\hh\pom.xml to C:\Users\arun.sudhakaran\.m2\repository\com\hh\sukku\hh\1.0.0\hh-1.0.0.pom
[INFO]
[INFO] --------------------< com.hh.sukku:common-service >---------------------
[INFO] Building common-service 1.0.0 [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ common-service ---
[INFO] Deleting D:\hh\haraji\workspace\hh\common-service\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ common-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ common-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to D:\hh\haraji\workspace\hh\common-service\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ common-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ common-service ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ common-service ---
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ common-service ---
[INFO] Building jar: D:\hh\haraji\workspace\hh\common-service\target\common-service-1.0.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ common-service ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ common-service ---
[INFO] Installing D:\hh\haraji\workspace\hh\common-service\target\common-service-1.0.0.jar to C:\Users\arun.sudhakaran\.m2\repository\com\hh\sukku\common-service\1.0.0\common-service-1.0.0.jar
[INFO] Installing D:\hh\haraji\workspace\hh\common-service\pom.xml to C:\Users\arun.sudhakaran\.m2\repository\com\hh\sukku\common-service\1.0.0\common-service-1.0.0.pom
[INFO]
[INFO] --------------------< com.hh.sukku:account-service >--------------------
[INFO] Building account-service 1.0.0 [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ account-service ---
[INFO] Deleting D:\hh\haraji\workspace\hh\account-service\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ account-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ account-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to D:\hh\haraji\workspace\hh\account-service\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,39] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,1] static import only from classes and interfaces
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[18,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[35,31] cannot find symbol
symbol: class Response
location: class com.hh.sukku.account.controller.UserController
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] hh 1.0.0 ........................................... SUCCESS [ 3.983 s]
[INFO] common-service ..................................... SUCCESS [ 6.801 s]
[INFO] account-service .................................... FAILURE [ 2.462 s]
[INFO] application-service 1.0.0 .......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.509 s
[INFO] Finished at: 2023-04-04T15:33:17+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project account-service: Compilation failure: Compilation failure:
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,39] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,1] static import only from classes and interfaces
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[18,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[35,31] cannot find symbol
[ERROR] symbol: class Response
[ERROR] location: class com.hh.sukku.account.controller.UserController
[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
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :account-service
我们甚至在
maven clean install -X
上分别尝试了maven install
和account-service
。所有尝试都因相同的错误而失败。
有趣的是,应用程序可以成功启动,但是当我们尝试访问 API 时,我们得到了
404
。可能缺少什么?没有可见的编译错误。
另一个有趣的事实是,当我们尝试将控制器从
account-service
导入到 application-service
并尝试构建 application-service
时,它是成功的。但是当我们尝试从 common-service
导入一个 bean 时,同样的失败了,所以我们可以缩小问题的范围,即 common-service
.
edit-1 的重大变化
groupId
与父模块不一样,所以我们创建了一个新模块,所有模块都具有相同的groupId
。问题保持不变。Response
选项导入 Fix project setup...
bean,并在弹出窗口中找到该 bean。问题保持不变。Create a simple project (skip archetype selection)
和Add project(s) to the working set
添加了新模块,但问题仍然存在。reference)的配置文件部分删除了
pom
,但问题仍然存在。但是在某些时候,当我们启动应用程序服务时,我们能够处理请求并将数据插入表中,因此我们尝试构建父 pom,但再次失败。后来应用程序开始再次为相同的请求提供 404。