Maven错误,PKIX路径构建失败:无法找到请求目标的有效认证路径。

12

有两个远程服务器。假设我们将它们命名为 A 和 B。 我的 Java 代码已经提交并在 GIT 存储库中更新。 我在 A 和 B 服务器上都拉取了代码。当我在服务器 A 上使用 mvn clean package install 时,它可以正常工作并成功构建。 但是,当我在服务器 B 上使用 mvn clean package install 时,它会给出以下错误并导致构建失败。

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] oagi-srt ........................................... SUCCESS [  0.292 s]
[INFO] oagi-srt-common .................................... SUCCESS [  1.959 s]
[INFO] oagi-srt-repository ................................ SUCCESS [  2.357 s]
[INFO] oagi-srt-service ................................... SUCCESS [  2.064 s]
[INFO] oagi-srt-import .................................... SUCCESS [  3.064 s]
[INFO] oagi-srt-webapp .................................... FAILURE [ 21.398 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 31.539 s
[INFO] Finished at: 2017-08-10T11:50:10+00:00
[INFO] Final Memory: 44M/303M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project srt-webapp: Could not resolve dependencies for project org.oagi:srt-webapp:war:1.0.0: Failed to collect dependencies at org.joinfaces:jsf-spring-boot-starter:jar:2.2.7 -> org.primefaces.extensions:all-themes:jar:1.0.8 -> org.primefaces.themes:afterdark:jar:1.0.8: Failed to read artifact descriptor for org.primefaces.themes:afterdark:jar:1.0.8: Could not transfer artifact org.primefaces.themes:themes-project:pom:1.0.8 from/to prime-repo-new (http://repository.primefaces.org): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [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/DependencyResolutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :srt-webapp

注意:如果我直接将项目war文件放置在Tomcat中并重新启动服务器,则我的应用程序在两个服务器上都能按照我预期的方式正常工作。
6个回答

32

我的公司IT政策阻止了Maven仓库的HTTPS访问,因为证书的过期日期很短。导入证书非常困难,所以我使用了以下方法来解决:

  • 升级到最新的Maven(验证版本为3.6.3)
  • 添加这些参数
mvn clean package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true

这个命令真是救命稻草。非常感谢。 - Ev.Rei.

2

谢谢Luis,你的建议帮了我很多。我交叉检查了证书环境,发现服务器B上缺少一些证书。 - Swagat

2
I get this problem when my JRE does not have the correct certificate to get through my companies proxy and out to the repo.
当我的JRE没有正确的证书通过公司代理并连接到仓库时,我会遇到这个问题。
Step 1将取决于您的IT部门。 您需要找到使您通过https代理的.crt文件,并将其下载为Your_Proxy.crt。
第2步是运行“mvn-version”以确保您知道maven正在使用哪个JRE。
第3步是使用Windows上的Powershell或Linux上的sudo以管理员模式进入JRE文件夹。
./bin/keytool.exe -importcert -ckeystore cacerts -file Your_Proxy.crt -alias Add_name_here

...你需要知道你的密钥库密码,通常默认为“changeit”


1
我运行mvn clean install时遇到了相同的错误,但对我来说解决方法完全不同...
我的maven settings.xml文件中有一个代理条目,例如:
<proxies>
  <proxy>
    ... 
  </proxy>
</proxies>

我尝试将证书导入到我的cacerts中,但这些都没有帮助。

只有在我删除了上述代理条目后,它才起作用。(可能是公司的代理设置发生了变化,因此我之前设置的上述内容已经无效)


0
如果您使用的是除了Apache Maven仓库之外的其他仓库,请尝试在URL中使用http而不是https

通常服务器会强制重定向到HTTPS。 - EM-Creations
如何避免这种情况?我的意思是服务器强制使用HTTPS。 - Akshay Joshi
@AkshayJoshi 在 <mirror><url>...</url></mirror> 中,需要使用 http 指定 URL,但如果重定向到 https,则此方法将无法正常工作。 - Eric
谢谢Eric。在我的情况下,问题是pom.xml中使用了<repositories>标签,它覆盖了settings.xml中的配置。从pom.xml中删除该repositories标签解决了这个问题。 - Akshay Joshi

0

我知道这个帖子已经老了。也许这是为其他可能会遇到此问题的用户提供答案的。

EM-Creations是正确的。使用http将导致默认重定向到https。我在stackoverflow和其他网站上搜索了几个问题,正确的答案是以某种方式使maven信任我们想要使用的存储库/ URL /站点的证书。

首先,我们需要导入该部分站点的(根)证书。请按照此链接进行操作 https://www.comodo.com/support/products/authentication_certs/setup/mac_chrome.php

现在,maven默认使用java keystore受信任的证书,并且默认情况下受信任的证书位于{JAVA_HOME}/jre/lib.security/cacerts。因此,下一步是将我们下载的证书添加到cacerts中。为此,您可以使用openssl或keytool命令。我个人更喜欢keytool,这里是一个示例

keytool 
-import 
-storepass <STOREPASS_PASSWORD> 
-noprompt 
-alias < unique  certificate alias for Keystore> 
-keystore <JRE_HOME>/lib/security/cacerts 
-trustcacerts 
-file <certificate file>

对于 Mac 用户,默认执行会导致权限被拒绝的错误。因此,您可以使用sudo使其可执行和可写。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接