问题描述
最近遇到了一个奇怪的问题,需要使用以下 mvn 依赖:
<dependency>
<groupId>org.alfresco</groupId>
<artifactId>alfresco-core</artifactId>
<version>7.3.0</version> <!-- 替换为你需要的版本 -->
</dependency>
直接尝试从 Maven 中央仓库下载,报错,因为这个依赖实际上是在 这个仓库。
按照 AI 的建议,在 Maven 的全局配置和用户配置中都添加了该仓库:
<profile>
<id>my-profile</id>
<repositories>
<!-- ... -->
<!-- Alfresco 公共仓库 -->
<repository>
<id>alfresco-public</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
但是尝试用 mvn 安装依赖(mvn clean install
)会出现以下错误:
Error:java: 读取C:\Users\70748\.m2\repository\org\activiti\activiti-spring-boot-starter\8.7.0\activiti-spring-boot-starter-8.7.0.jar时出错; zip END header not found
问题排查
首先直接访问了仓库 URL,确认仓库可以正常访问,而且在浏览器/URL index 模式下都可以正常查找到目标 jar 包的存放目录,说明远程 Maven 仓库和 jar 包资源都没有问题。
查看本地 Maven 仓库目录,jar 包确实被下载了,但是格式不对,用解压工具无法打开,提示文件格式错误。在 Windows-sub-Linux 下用file
命令查看 jar 包格式,发现实际上是一个 HTML 文件,用文本编辑器打开,发现是一些阿里的 CDN 资源链接(JS 和 CSS 之类的)。除此之外也没有其它有用信息。
到此,确认是 mvn 仓库配置有问题,依然使用 Maven 中央仓库下载依赖,而不是使用配置的 alfresco 公共仓库。而 Maven 中央仓库的镜像仓库(阿里云)中没有这个依赖,所以下载到阿里的错误页面(HTML)。
这里似乎表明阿里云的镜像仓库比较糙,查不到的话应该返回一个 404 页面才对。
问题解决
检查 Maven 配置文件,确保仓库的配置信息无误:
<profile>
<id>my-profile</id>
<repositories>
<!-- 显式声明 Maven 中央仓库 -->
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- Alfresco 公共仓库 -->
<repository>
<id>alfresco-public</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
这里配置两个 Maven 仓库:
-
Maven 中央仓库(central)
-
alfresco 公共仓库(alfresco-public)
默认情况下 Maven 就会使用 Maven 的中央仓库(central),但这样做会让人产生一些误解,导致配置出错,因此我这里选择将 Maven 中央仓库显式配置。
确保仓库的镜像配置无误:
需要注意的是,镜像配置中的mirrorOf
要与目标仓库的id
一致,比如这里配置的两个镜像仓库,它们的目标仓库都是中央仓库central
。
这里注释的部分是配置的阿里云的 alfresco 公共仓库的镜像仓库,但是该仓库不对外提供服务,只供内部员工使用,有身份验证机制,所以注释掉了。
大部分关于 Maven 配置的文章到这里都结束了,但是,这样做并不能让配置好的仓库生效,也是导致前面问题出现的原因。因为要让仓库配置生效,还需要:
<activeProfiles>
<activeProfile>my-profile</activeProfile>
</activeProfiles>
这里配置的activeProfile
要与前面的profile
中的id
一致:
这样做才能让仓库配置生效。
之所以国内大把 Maven 配置没有提这一点,但是配置阿里云仓库镜像能生效,是因为 Maven 默认就会使用中央仓库,相当于中央仓库的配置是默认激活的(Active),因此中央仓库的镜像也能生效。但是如果添加之外的 Maven 仓库,就需要在
activeProfiles
中将相应配置显式激活,否则该仓库就不会生效。
现在 Maven 就可以正常通过Alfresco
仓库正常下载依赖了。
如果还是存在问题,可以用一个终极大招,在项目的 POM 文件中添加:
<project>
<!-- ... -->
<repositories>
<!-- Alfresco 公共仓库 -->
<repository>
<id>alfresco-public</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</repository>
</repositories>
</project>
可以在该项目的建构中直接使用指定仓库。
最后,感谢 Deepseek 在本次定位和处理问题时的帮助。搜索引擎查到的结果几乎没有任何帮助,全靠 Deepseek。
The End.
文章评论