赞
踩
修改 Maven 默认的全局配置文件:
类 Unix
系统: Mac OS
/ Linux
默认在 ~/.m2/settings.xml
Windows
系统:一般在 Maven 安装目录的 conf/settings.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
-
- <!-- 添加以下这段 Mirror 配置 -->
- <mirrors>
- <!-- Aliyun -->
- <mirror>
- <id>aliyunmaven</id>
- <name>aliyun maven</name>
- <mirrorOf>*</mirrorOf>
- <url>https://maven.aliyun.com/repository/public</url>
- </mirror>
- </mirrors>
-
-
- </settings>
修改项目的 pom.xml
,添加以下内容:
- <repositories>
- <repository>
- <id>aliyunmaven</id>
- <url>https://maven.aliyun.com/repository/public</url>
- </repository>
- </repositories>
在 ${USER_HOME}/.gradle/
目录下创建 init.gradle
文件,添加以下内容:
- allprojects {
- repositories {
- def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
- all { ArtifactRepository repo ->
- if(repo instanceof MavenArtifactRepository){
- def url = repo.url.toString()
- if (url.startsWith('https://repo1.maven.org/maven2')) {
- project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
- remove repo
- }
- }
- }
- maven { url ALIYUN_REPOSITORY_URL }
- }
- }
修改项目的 build.gradle
文件,添加以下内容:
- buildscript {
- repositories {
- maven { url 'https://maven.aliyun.com/repository/public' }
- }
- }
-
- allprojects {
- repositories {
- maven { url 'https://maven.aliyun.com/repository/public' }
- }
- }
也可以直接添加在 repositories
内:
- repositories {
- maven { url 'https://maven.aliyun.com/repository/public' }
- mavenCentral()
- }
在上面的配置中,我们主要配置的是 public
仓库,而 阿里云 除此之外,还代理了许多其他的仓库,可作为下载源。如 google
、spring
等(更多可点进链接查看详情)。
而我们常用的 public
仓库,其实是 central 仓库和 jcenter 仓库的聚合,所以配置了 public
仓库,就不需要再配置 jcenter
的仓库了。
若需要使用其他的代理仓库,如 google
、spring
,可参考以下配置:
同上,修改 settings.xml
文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
-
- <mirrors>
- <!-- Aliyun -->
- <mirror>
- <id>alimaven-public</id>
- <name>aliyun public maven repo</name>
- <url>https://maven.aliyun.com/repository/public</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
-
- <mirror>
- <id>alimaven-google</id>
- <name>aliyun google maven repo</name>
- <url>https://maven.aliyun.com/repository/google</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
-
- <mirror>
- <id>alimaven-spring</id>
- <name>aliyun spring maven repo</name>
- <url>https://maven.aliyun.com/repository/spring</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
-
- <!-- Tencent Cloud -->
- <!-- <mirror>
- <id>nexus-tencentyun</id>
- <mirrorOf>*</mirrorOf>
- <name>Nexus tencentyun</name>
- <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
- </mirror> -->
-
- <!-- Netease 163 -->
- <!-- <mirror>
- <id>netease</id>
- <mirrorOf>*</mirrorOf>
- <name>Netease maven</name>
- <url>http://mirrors.163.com/maven/repository/maven-public/</url>
- </mirror> -->
- </mirrors>
-
-
- </settings>
-
修改 pom.xml
:
- <repositories>
- <repository>
- <id>aliyunmaven-google</id>
- <name>aliyun google maven repo</name>
- <url>https://maven.aliyun.com/repository/google</url>
- <layout>default</layout>
- <!-- 是否开启 release 版构件下载 -->
- <releases>
- <enabled>true</enabled>
- </releases>
- <!-- 是否开启 snapshot 版构件下载 -->
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>aliyunmaven-spring</id>
- <name>aliyun spring maven repo</name>
- <url>https://maven.aliyun.com/repository/spring</url>
- </repository>
- </repositories>
是否开启 releases
、snapshots
版下载可根据自身需求调整。
- allprojects {
- repositories {
- def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
- def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
- all { ArtifactRepository repo ->
- if(repo instanceof MavenArtifactRepository){
- def url = repo.url.toString()
- if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com')) {
- project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
- remove repo
- }
- if (url.startsWith('https://dl.google.com/dl/android/maven2')) {
- project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
- remove repo
- }
- }
- }
- maven { url ALIYUN_REPOSITORY_URL }
- maven { url ALIYUN_GOOGLE_URL }
- }
- }
修改 build.gradle
:
- buildscript {
- repositories {
- maven { url 'https://maven.aliyun.com/repository/google' }
- maven { url 'https://maven.aliyun.com/repository/spring' }
- }
- }
-
- allprojects {
- repositories {
- maven { url 'https://maven.aliyun.com/repository/google' }
- maven { url 'https://maven.aliyun.com/repository/spring' }
- }
- }
https://mirrors.cloud.tencent.com
参考配置:
- <mirror>
- <id>nexus-tencentyun</id>
- <mirrorOf>*</mirrorOf>
- <name>Nexus tencentyun</name>
- <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
- </mirror>
- <repositories>
- <repository>
- <id>nexus-tencentyun</id>
- <name>Nexus tencentyun</name>
- <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
- </repository>
- </repositories>
- buildscript {
- repositories {
- maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
- }
- }
-
- allprojects {
- repositories {
- maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
- }
- }
Q: 为什么你配置里用的 URL 都是 https://maven.aliyun.com/repository/public
,而我看其他的人/文章,都用的是 http://maven.aliyun.com/nexus/content/groups/public
呢?
A: 阿里云在前几年对 https://maven.aliyun.com 进行了代码和架构上的改造,新版的 https://maven.aliyun.com 下载速度更快,支持高并发,而且全站进行了 HTTPS 加密,更安全。而新版的 Maven Mirror 地址,就是 https://maven.aliyun.com/repository/{reponame}
,然而阿里云为了保持旧地址的兼容,所以旧地址依然可以使用,但推荐使用新地址。
更多详情可参考: 【FAQ】新版 maven.aliyun.com 答疑
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。