Spring Boot本地依赖导入失败求助:自定义依赖无法被识别
尝试创建包含本地依赖的Spring Boot应用,已生成名为generateproto的本地依赖项目,其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.1.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>generateproto</artifactId> <version>0.0.1-SNAPSHOT</version> <name>generate-proto</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.23.2</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>1.16.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> <version>1.16.1</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>1.55.1</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>1.16.1</version> </dependency> <dependency> <!-- necessary for Java 9+ --> <groupId>org.apache.tomcat</groupId> <artifactId>annotations-api</artifactId> <version>6.0.53</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> </dependencies> <repositories> <repository> <id>repositorio.local</id> <name>Meu repositorio Local</name> <url>file:../maven-repositorio</url> </repository> </repositories> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.1</version> </extension> </extensions> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact> com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier} </protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact> io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier} </pluginArtifact> <clearOutputDirectory>false</clearOutputDirectory> <outputDirectory>src/main/java</outputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
执行以下命令将该依赖安装到本地仓库:
mvn install:install-file -Dfile=target/generateproto-0.0.1-SNAPSHOT.jar -DgroupId=com.example -DartifactId=generateproto -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DlocalRepositoryPath=../maven-repositorio
在另一个Spring Boot项目的pom.xml中引入该依赖:
<dependency> <groupId>com.example</groupId> <artifactId>generateproto</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
文件夹结构:
- another project
- local dependency(即generateproto项目)
- maven-repositorio
无法导入该本地依赖的类,执行mvn dependency:purge-local-repository时出现错误:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-dependency-plugin:3.5.0:purge-local-repository
(default-cli) on project cart-purchase-command: Failed to refresh
project dependencies for:
com.example:cart-purchase-command:jar:0.0.1-SNAPSHOT: required
artifacts missing: [ERROR]
com.example:generateproto:jar:0.0.1-SNAPSHOT
1. 消费项目未配置本地仓库地址
另一个Spring Boot项目(cart-purchase-command)的pom.xml中没有声明自定义本地仓库repositorio.local,Maven默认只会从中央仓库和默认本地仓库(~/.m2/repository)查找依赖,无法定位到你安装在../maven-repositorio里的包。
解决:在消费项目的pom.xml中添加仓库配置:
<repositories> <repository> <id>repositorio.local</id> <name>Meu repositorio Local</name> <url>file:./maven-repositorio</url> </repository> </repositories>
注意路径要匹配消费项目的位置,消费项目在another project根目录,./maven-repositorio指向同级的maven-repositorio文件夹。
2. Spring Boot插件打包的是可执行Jar,无法作为依赖使用
generateproto项目中使用的spring-boot-maven-plugin,默认打包的是可执行Jar,其内部类文件存放在BOOT-INF/classes目录下,不符合普通依赖Jar的结构,Maven无法识别其中的类。
解决:
- 如果
generateproto只是一个依赖库,不需要独立运行,直接移除spring-boot-maven-plugin; - 如果需要保留可执行能力,修改插件配置生成普通依赖Jar:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> </plugin>
修改后打包会生成两个Jar:generateproto-0.0.1-SNAPSHOT.jar(普通依赖Jar)和generateproto-0.0.1-SNAPSHOT-exec.jar(可执行Jar),执行install命令时需指定安装普通Jar文件。
3. 检查依赖是否正确安装到目标仓库
执行install命令后,手动检查another project/maven-repositorio/com/example/generateproto/0.0.1-SNAPSHOT目录下是否存在以下文件:
generateproto-0.0.1-SNAPSHOT.jargenerateproto-0.0.1-SNAPSHOT.pom
如果缺少pom文件,Maven会无法解析依赖。建议直接在generateproto项目根目录执行mvn install -Dmaven.repo.local=../maven-repositorio,该命令会自动生成并安装pom和jar文件,比手动执行install:install-file更可靠。
4. SNAPSHOT版本缓存问题
SNAPSHOT依赖会被Maven缓存,可能导致消费项目无法获取最新安装的版本。可以执行以下命令强制更新依赖:
mvn clean install -U
-U参数强制Maven更新SNAPSHOT依赖。
内容的提问来源于stack exchange,提问作者Rafael Souza

