Gradle配置国内镜像

默认使用Gradle进行依赖更新时,往往会从https://repo.maven.apache.org/maven2下载依赖,这个地址在国内不太友好,使用国内镜像地址更加快捷:

Gradle全局配置

在 ${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()
}

更多:https://www.cnblogs.com/chansblogs/p/9577931.html

遇到 "java.io.IOException: Downloading from [url] failed: timeout" 这样的错误通常是网络下载超时或者连接不稳定造成的。要解决这个问题,你可以尝试以下几个步骤: 1. **检查网络**:确保你的设备连上的是稳定、有足够带宽的网络。如果在公共Wi-Fi环境下,有时可能会因为限制或者服务器压力大而发生超时。 2. **增加超时时间**:在Gradle构建脚本中,你可以调整`buildscript.repositories`部分下载仓库的超时时间。例如: ```groovy repositories { mavenCentral { url 'https://repo1.maven.org/maven2' timeout '500000' // 将默认的超时时间从10秒(10000ms)增加到5分钟(500000ms) } } ``` 3. **分段下载**:有些大型文件可能会因单次请求过大导致超时,可以考虑将下载任务分解成几个小块。 4. **清理缓存**:清除 Gradle 缓存并重新下载,有时候旧的缓存可能会干扰新的下载过程。可以在用户目录下的 `.gradle` 文件夹里查找 `caches` 子目录删除。 5. **检查防火墙设置**:确保防火墙没有阻止对指定URL的访问。如果是企业环境,可能需要联系管理员确认安全策略。 6. **更换镜像源**:如果你在国外,可以试试国内Gradle 镜像如阿里云的 Maven Central 镜像。 7. **联系支持**:如果以上都试过还是无法解决,可能是服务器端的问题,此时应该联系 Gradle 官方或者第三方软件包提供者的技术支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值