最近换了一台电脑修改以前的老MAVEN项目,突然爆出了这个错误:
Failure to transfer org.apache.maven.plugins:maven-assembly-plugin:pom:3.1.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-assembly-plugin:pom:3.1.0 from/to central (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version pom.xml /tester line 1 Maven Configuration Problem
分析:项目在原电脑正常运行,排除是项目有问题,那就只能是环境的问题,经过排查,发现maven无法下载jar吧,网页打开地址(https://repo.maven.apache.org/maven2),可以,排除网络的问题,经过多方搜查,原来是由于TLSv1.1 协议不安全, 出于PCI 安全标准的原因, 自2018-06-18起, maven Sonatype 中央仓库不再支持 TLSv1.1 以及以下的协议版本,原文参见 no longer supports TLSv1.1 and below 官方说明通过chrome security 面板看到, maven 中央仓库已经是 TLSv1.2 协议了。
下表格是 根据Oracle文档 各版本JDK默认使用的TLS协议:
JDK 8 (March 2014 to present) | JDK 7 (July 2011 to present) | JDK 6 (2006 to end of 2013) | |
TLS Protocols | TLSv1.2 (default) TLSv1.1 TLSv1 SSLv3 | TLSv1.2 TLSv1.1 TLSv1 (default) SSLv3 | TLS v1.1 (JDK 6 update 111+) TLSv1 (default) SSLv3 |
有两种解决方法:
第一种方法
将jdk版本升级到jdk1.8,因为在1.8及以上版本,才会默认使用TLS1.2。
注意:是本地JAVA_HOME配置的JDK8,不是开发工具配置或者项目配置的JDK8
第二种方法
使用 http 去访问 maven 中央仓库
对于安全要求不严格的场景, 可以使用http 去访问maven 仓库
对于 plugin , 可以在 setting .xml 加入或者改成如下:
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
下面是相关位置代码:
<profiles>
<profile>
<id>maven-home</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
</profile>
第三种方法
通过添加 -Dhttps.protocols=TLSv1.2 , 配置 java runtime 参数,来 enable TLS 1.2
如果你是jdk 1.7 (尤其是 1.7.0_131-b31 以前的版本)你可以使用以下command line:
mvn -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 <goals>
或者在相关脚本中加入:
export MAVEN_OPTS=-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2
如果你是 1.6 的版本, sorry , 只能使用http 模式, 虽然有部分jdk 1.6 小版本声称支持 TLS 1.2,参见
Changes in 6u115 b32 TLS v1.2 support now available
参考
https://central.sonatype.org/articles/2018/May/04/discontinued-support-for-tlsv11-and-below/
https://stackoverflow.com/questions/50946420/could-not-transfer-artifact-https-repo-maven-apache-org-maven2-received-fat/50956622