在 Maven 的 pom.xml
中直接配置镜像仓库并不是推荐做法(Maven 镜像一般通过全局配置文件 settings.xml
设置),但如果你需要强制为当前项目配置阿里云镜像,可以按以下步骤操作:
方法 1️⃣:推荐方式(全局配置,修改 settings.xml
)
-
找到 Maven 的全局配置文件
settings.xml
:-
Windows:
C:\Users\你的用户名\.m2\settings.xml
-
Linux/macOS:
~/.m2/settings.xml
-
如果文件不存在,直接新建。
-
-
添加阿里云镜像配置:
<settings> <mirrors> <mirror> <id>aliyunmaven</id> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> <!-- 覆盖 Maven 中央仓库 --> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>
方法 2️⃣:项目级配置(修改 pom.xml
,不推荐)
如果必须通过 pom.xml
配置,需在 <repositories>
中直接指定阿里云仓库:
<project>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyunmaven</id>
<name>阿里云插件仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
⚠️ 注意事项
-
优先级问题:
-
settings.xml
的配置优先级高于pom.xml
,推荐用方法 1 全局配置。 -
如果公司有私有仓库,需在
settings.xml
中排除阿里云镜像:<mirrorOf>central,!company-repo</mirrorOf>
-
-
验证配置是否生效:
mvn clean compile
观察下载依赖的 URL 是否来自maven.aliyun.com