这可能是由于默认偏好使用 IPv6 造成的。你可以发现它添加
-e
参数到你的maven命令(如mvn clean install -e
) - 它会打印失败的堆栈跟踪。在这种情况下,它看起来像:
Caused by: java.net.UnknownHostException: repo.maven.apache.org: nodename nor servname provided, or not known
at java.net.Inet6AddressImpl.lookupAllHostAddr (Native Method)
at java.net.InetAddress$PlatformNameService.lookupAllHostAddr (InetAddress.java:933)
at java.net.InetAddress.getAddressesFromNameService (InetAddress.java:1529)
at java.net.InetAddress$NameServiceAddresses.get (InetAddress.java:852)
at java.net.InetAddress.getAllByName0 (InetAddress.java:1519)
at java.net.InetAddress.getAllByName (InetAddress.java:1377)
at java.net.InetAddress.getAllByName (InetAddress.java:1305)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve (SystemDefaultDnsResolver.java:45)
...
要解决这个问题,请通过文件添加maven参数
.mvn/jvm.config
(如果不存在则创建):
-Djava.net.preferIPv4Stack=true
顺便说一句,您可以将此文件用于其他 Maven jvm 选项,例如
-Djava.awt.headless=true -Xmx1024m -Djava.net.preferIPv4Stack=true