gradle全局替换国内源方案

一、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"
       }

其他参考
国内源
其他汇总
方式1

总结

我也不知道哪样管用, 先用着

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值