Spring Boot创建Maven依赖并引用时编译报错的问题排查
问题
尝试创建一个可供其他Spring Boot项目引用的Maven依赖,common项目构建成功,但sso项目引用该依赖后编译失败,报错信息如下:
[INFO] [1m--- [0;32mcompiler:3.13.0:compile[m [1m(default-compile)[m @ [36msso[0;1m ---[m [INFO] Recompiling the module because of [1mchanged source code[m. [INFO] Compiling 3 source files with javac [debug parameters release 21] to target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /D:/workspaces/test/sso/src/main/java/ir/com/sso/domain/entity/User.java:[3,35] package ir.com.common.domain.entity does not exist [ERROR] /D:/workspaces/test/sso/src/main/java/ir/com/sso/domain/entity/User.java:[20,27] cannot find symbol symbol: class BaseEntity [INFO] 2 errors [INFO] ------------------------------------------------------------- [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;31mBUILD FAILURE[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 5.267 s [INFO] Finished at: 2024-12-06T10:06:06+03:30 [INFO] [1m------------------------------------------------------------------------[m [ERROR] Failed to execute goal [32morg.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile[m [1m(default-compile)[m on project [36msso[m: [1;31mCompilation failure[m: Compilation failure: [ERROR] /D:/workspaces/test/sso/src/main/java/ir/com/sso/domain/entity/User.java:[3,35] package ir.com.common.domain.entity does not exist [ERROR] /D:/workspaces/test/sso/src/main/java/ir/com/sso/domain/entity/User.java:[20,27] cannot find symbol [ERROR] symbol: class BaseEntity [ERROR] -> [1m[Help 1][m [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the [1m-e[m switch. [ERROR] Re-run Maven using the [1m-X[m switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [1m[Help 1][m http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
common项目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>3.4.0</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>ir.com</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <packaging>pom</packaging> <description>common project for extend</description> <properties> <java.version>21</java.version> </properties> <dependencies>...</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>
sso项目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>3.4.0</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>ir.com</groupId> <artifactId>sso</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sso</name> <description>Demo project for SSO</description> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>ir.com</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <type>pom</type> </dependency> ... </project>
解决方案
1. 修改common项目的打包类型
将common项目pom.xml中的<packaging>pom</packaging>改为<packaging>jar</packaging>。pom打包适用于聚合项目或父项目,不会编译生成包含类文件的jar包;jar打包才会编译代码并生成可供其他项目引用的jar。
2. 修改sso项目的依赖配置
移除sso依赖中的<type>pom</type>,默认依赖类型即为jar,指定pom类型只会引入common的依赖配置,无法获取其中的Java类。修改后的依赖配置如下:
<dependency> <groupId>ir.com</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
3. 重新构建并安装common项目
在common项目根目录执行命令:
mvn clean install
该命令会编译代码、生成jar包并安装到本地Maven仓库,确保sso项目能找到该依赖。
4. 构建sso项目
在sso项目根目录执行命令:
mvn clean compile
此时项目应能正常编译,不再出现找不到包和类的错误。
内容的提问来源于stack exchange,提问作者Albert
相关产品推荐
相关产品推荐

