Maven:解决IDEA无法下载源码

问题

通过点击进IDEA反编译的代码文件处下载源代码Sources:


image.png

IDEA提示报错,无法下载源代码:


image.png

解决办法
一、下载全部依赖的源码
mvn dependency:resolve是解决项目的所有依赖项,它将打印出已解决依赖项的详细信息。指定可选参数classifier=sources,将尝试分析下载对应的依赖的源代码。打开Teminal,使用本地的Maven执行,然后在下载就有了。

mvn dependency:resolve -Dclassifier=source

二、按需下载
先去确认 IDEA 的 File | Settings | Build, Execution, Deployment | Build Tools | Maven设置,Maven版本是3.8.4,配置文件也是3.8.4的,本地Maven仓库也有设置。


image.png

再次尝试下载源代码,如果失败,去查看本地的Maven仓库有没有一个后缀为sources.jar.lastUpdated的文件,这个文件是Maven尝试下载源代码但是失败了的记录文件。


image.png

使用notepad++打开该文件,根据里面的内容,看来无法下载是和这个maven-default-http-blocker有很大的关系。
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Sun Nov 21 16:52:48 CST 2021
http\://0.0.0.0/.error=Could not transfer artifact org.slf4j\:slf4j-api\:jar\:sources\:1.7.21 from/to maven-default-http-blocker (http\://0.0.0.0/)\: Blocked mirror for repositories\: [nexus-aliyun (http\://maven.aliyun.com/nexus/content/groups/public, default, releases)]
@default-maven-default-http-blocker-http\://0.0.0.0/.lastUpdated=1637484768579

查看setting.xml文件,找到了以下相关的配置,上面说是这个maven-default-http-blocker阻碍了去阿里的镜像库查找依赖,注释掉这个maven-default-http-blocker的镜像,并且最好多找几个镜像加上去,可以防止一些镜像没有某个依赖导致报错。


image.png

保存后重启IDEA。

常用镜像

<!-- 阿里镜像 -->
<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<mirror>
    <id>repo2</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://repo2.maven.org/maven2/</url>
</mirror>

<mirror>
    <id>ibiblio</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>

<mirror>
    <id>jboss-public-repository-group</id>
    <mirrorOf>central</mirrorOf>
    <name>JBoss Public Repository Group</name>
    <url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>

<mirror>
    <id>google-maven-central</id>
    <name>Google Maven Central</name>
    <url>https://maven-central.storage.googleapis.com
    </url>
    <mirrorOf>central</mirrorOf>
</mirror>

<!-- 中央仓库在中国的镜像 -->
<mirror>
    <id>maven.net.cn</id>
    <name>oneof the central mirrors in china</name>
    <url>http://maven.net.cn/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容