gradle设置全局国内镜像

本文介绍如何通过修改%userprofile%/.gradle/init.gradle文件来优化Gradle项目的依赖管理及缓存策略,包括替换默认的Maven仓库为阿里云仓库、设置本地缓存等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 %userprofile%/.gradle 路径下,新建init.gradle文件,编辑写入:

def replacer = [
    'http[s]://repo1.maven.org/maven2': 'https://maven.aliyun.com/repository/public',
    'http[s]://jcenter.bintray.com': 'https://maven.aliyun.com/repository/public',
    'http[s]://maven.google.com': 'https://maven.aliyun.com/repository/google',
    'http[s]://plugins.gradle.org/m2': 'https://maven.aliyun.com/repository/gradle-plugin',
    'http[s]://repo.spring.io/libs-milestone': 'https://maven.aliyun.com/repository/spring',
    'http[s]://repo.spring.io/plugins-release': 'https://maven.aliyun.com/repository/spring-plugin',
    'http[s]://repo.grails.org/grails/core': 'https://maven.aliyun.com/repository/grails-core',
    'http[s]://repository.apache.org/snapshots': 'https://maven.aliyun.com/repository/apache-snapshots',
]

gradle.settingsEvaluated { settings ->
    settings.buildCache {
        local {
            directory = new File('E:\\temp\\gradle')
            removeUnusedEntriesAfterDays = 30
        }
    }
    settings.pluginManagement {
        repositories {
            replacer[mavenCentral().url.toString().replaceAll('http[s]', 'http[s]')] = 'https://maven.aliyun.com/repository/public'
            replacer[jcenter().url.toString().replaceAll('http[s]', 'http[s]')] = 'https://maven.aliyun.com/repository/public'
            replacer[google().url.toString().replaceAll('http[s]', 'http[s]')] = 'https://maven.aliyun.com/repository/google'
            gradlePluginPortal()
            all { repo ->
                if (repo instanceof MavenArtifactRepository) {
                    replacer.each { k, v ->
                        if (repo.url.toString().matches(k)) {
                            repo.url = v
                        }
                    }
                }
            }
        }
    }
}

allprojects {
    repositories {
        all { repo ->
            if (repo instanceof MavenArtifactRepository) {
                replacer.each { k, v ->
                    if (repo.url.toString().matches(k)) {
                        repo.url = v
                    }
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值