SpringBoot多模块配置Maven依赖后无法导入OwnerService问题

问题现象
- 在
DataLoader类中无法实例化OwnerService,根因是无法从pet-clinic-data模块导入对应包。 - 已经在
pet-clinic-web模块的pom文件中声明了pet-clinic-data依赖,导入依然失败。
现有配置
pet-clinic-data 模块 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>sfg-pet-clinic</artifactId> <groupId>guru.springframework</groupId> <version>0.0.2</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pet-clinic-data</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <spring-boot.repackage.skip>true</spring-boot.repackage.skip> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <skip>true</skip> </configuration> </execution> </executions> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
pet-clinic-web 模块 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>sfg-pet-clinic</artifactId> <groupId>guru.springframework</groupId> <version>0.0.2</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pet-clinic-web</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <artifactId>pet-clinic-data</artifactId> <groupId>guru.springframework</groupId> <version>0.0.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>guru.springframework</groupId> <artifactId>pet-clinic-data</artifactId> <version>0.0.2</version> <scope>compile</scope> </dependency> </dependencies> </project>
问题原因与修复步骤
- 先清理重复依赖:
pet-clinic-web的pom里重复声明了两次pet-clinic-data依赖,删掉其中任意一个即可,重复声明不会增强依赖效果,反而可能引发解析异常。 - 核心问题是打包配置错误:
pet-clinic-data作为公共依赖模块,被配置了Spring Boot的repackage打包逻辑,虽然写了skip配置,但配置位置在execution内部,实际没有生效,最终模块被打包成了Spring Boot可执行fat jar——这种jar的类文件都放在BOOT-INF/classes路径下,普通Maven依赖引用时无法识别到里面的类,自然会报导入失败。
修复方式二选一:- 直接删掉
pet-clinic-data模块里spring-boot-maven-plugin的全部配置,公共模块不需要打可执行包,Maven默认打出来的普通jar就能被其他模块正常依赖。 - 如果要保留插件配置,就把skip配置移到插件全局层级,不要放在execution的配置里。
- 直接删掉
- 配置改完后,在项目根目录执行
mvn clean install,把修正后的pet-clinic-data安装到本地Maven仓库,再刷新IDE的Maven依赖、重新导入项目即可正常导入类。
内容的提问来源于stack exchange,提问作者Epitaph
相关产品推荐
相关产品推荐

