一、init.gradle
创建init.gradle放在GRADLE_USER_HOME目录下
allprojects {
repositories {
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url1 = repo.url.toString()
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin'
def repositoriesToReplace = [
'https://repo1.maven.org/maven2/': ALIYUN_REPOSITORY_URL,
'https://jcenter.bintray.com/': 'https://maven.aliyun.com/repository/jcenter', // 注意:jcenter已停止服务
'https://dl.google.com/dl/android/maven2/': ALIYUN_GOOGLE_URL,
'https://plugins.gradle.org/m2/': ALIYUN_GRADLE_PLUGIN_URL
]
repositoriesToReplace.each { defaultUrl, aliyunUrl ->
if (url1.startsWith(defaultUrl)) {
project.logger.lifecycle "Repository ${repo.url} replaced by $aliyunUrl."
remove(repo)
project.logger.lifecycle "maven ${url} $aliyunUrl."
maven { url aliyunUrl}
}
}
}
}
}
}
另一种实现
fun RepositoryHandler.enableMirror() {
all {
if (this is MavenArtifactRepository) {
val originalUrl = this.url.toString().removeSuffix("/")
urlMappings[originalUrl]?.let {
logger.lifecycle("Repository[$url] is mirrored to $it")
this.setUrl(it)
}
}
}
}
val urlMappings = mapOf(
"https://repo.maven.apache.org/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://dl.google.com/dl/android/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)
gradle.allprojects {
buildscript {
repositories.enableMirror()
}
repositories.enableMirror()
}
gradle.beforeSettings {
pluginManagement.repositories.enableMirror()
dependencyResolutionManagement.repositories.enableMirror()
}
二、gradle.properties
创建gradle.properties放在GRADLE_USER_HOME目录下
# gradle.properties 示例配置
# 使用阿里云 Maven 镜像
repositories.grails.default = https://maven.aliyun.com/repository/public
repositories.grails.default.1 = https://mirrors.tuna.tsinghua.edu.cn/maven/repos/public
repositories.grails.default.2 = https://repo.maven.apache.org/maven2
# 设置阿里云的 Maven 仓库作为镜像
systemProp.maven.repo.local=https://maven.aliyun.com/repository/public
# 设置阿里云的 JCenter 仓库作为镜像
systemProp.jcenter.url=https://maven.aliyun.com/repository/jcenter
# 设置阿里云的 Google Maven 仓库作为镜像
systemProp.google.maven.repo.url=https://maven.aliyun.com/repository/google
# 设置阿里云的 Gradle 插件仓库作为镜像
systemProp.gradle.plugin.repo.url=https://maven.aliyun.com/repository/gradle-plugin
# 其他可能的配置
#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1080
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1080
# 使用清华大学的 Maven 镜像
# repositories.grails.default = https://mirrors.tuna.tsinghua.edu.cn/maven/repos/public
# 使用中科大的 Maven 镜像
#repositories.grails.default = https://mirrors.ustc.edu.cn/maven/
三、项目使用方式
可能出错时使用吧
maven {
allowInsecureProtocol = true
url = "https://maven.aliyun.com/repository/central"
}
maven {
allowInsecureProtocol = true
url = "https://mirrors.huaweicloud.com/repository/maven/"
}
maven {
allowInsecureProtocol = true
url = "https://maven.aliyun.com/repository/google"
}
maven {
allowInsecureProtocol = true
url = "https://maven.aliyun.com/repository/central"
}
maven {
allowInsecureProtocol = true
url = "https://maven.aliyun.com/repository/gradle-plugin"
}
总结
我也不知道哪样管用, 先用着