Spring Boot项目执行mvn build package时依赖解析失败求助
我是Spring Boot新手,执行mvn build package命令构建项目时遇到错误。
已尝试的解决方法
- Maven更新项目
- 执行
mvn -u clean install
但问题仍未解决,恳请帮助。
错误信息
[ERROR] Failed to execute goal on project openapi-generator-online:
Could not resolve dependencies for project
org.openapitools:openapi-generator-online:jar:6.3.0-SNAPSHOT: Failed
to collect dependencies at
org.openapitools:openapi-generator:jar:6.3.0-SNAPSHOT: Failed to read
artifact descriptor for
org.openapitools:openapi-generator:jar:6.3.0-SNAPSHOT: Could not
transfer artifact
org.openapitools:openapi-generator:pom:6.3.0-SNAPSHOT from/to
sonatype-snapshots: Transfer failed for [对应pom文件路径]:
PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target -> [Help 1]
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-project</artifactId> <!-- RELEASE_VERSION --> <version>6.3.0-SNAPSHOT</version> <!-- /RELEASE_VERSION --> <relativePath>../..</relativePath> </parent> <artifactId>openapi-generator-online</artifactId> <packaging>jar</packaging> <name>openapi-generator-online</name> <properties> <spring-boot.version>2.7.5</spring-boot.version> <springfox-version>3.0.0</springfox-version> <sonar.exclusions>**/org/openapitools/codegen/online/**/*</sonar.exclusions> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>openapi-generator-online</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <configuration> <configLocation>${project.parent.basedir}${file.separator}google_checkstyle.xml</configLocation> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>static-analysis</id> <build> <plugins> <plugin> <groupId>com.github.spotbugs</groupId> <artifactId>spotbugs-maven-plugin</artifactId> <configuration> <excludeFilterFile>${project.parent.basedir}${file.separator}spotbugs-exclude.xml</excludeFilterFile> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> </plugin> <plugin> <groupId>se.bjurr.violations</groupId> <artifactId>violations-maven-plugin</artifactId> <configuration> <!-- Should be decreased regularly down to 0 as issues are fixed. --> <maxViolations>3</maxViolations> </configuration> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpringFox dependencies --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox-version}</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency> <!-- Bean Validation API support --> <dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> </dependency> <dependency> <groupId>org.openapitools</groupId> <artifactId>openapi-generator</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </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-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
解决方案
方法1:导入Sonatype仓库SSL证书到JVM信任库
- 下载Sonatype快照仓库的SSL证书:通过浏览器访问仓库地址,导出DER编码的X.509格式证书。
- 用
keytool命令导入证书:
信任库默认密码为keytool -import -alias sonatype-snapshots -keystore $JAVA_HOME/jre/lib/security/cacerts -file 证书文件路径changeit,按提示输入密码并确认导入。
方法2:临时跳过SSL证书验证(仅测试环境用)
在Maven命令中添加参数跳过SSL校验:
mvn clean package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
方法3:检查网络代理设置
如果使用代理,确保Maven的settings.xml配置了正确代理信息,且代理的SSL证书已导入信任库。
方法4:本地构建依赖
因为项目依赖的是6.3.0-SNAPSHOT版本的openapi-generator,可先从源码构建并安装到本地仓库:
- 克隆openapi-generator源码仓库,切换到对应版本分支。
- 执行
mvn clean install将依赖安装到本地。 - 回到当前项目执行构建命令。
内容的提问来源于stack exchange,提问作者Aniket Thorat

