Windows使用Gradle构建项目时工件下载过慢的解决方式

Author Avatar
Administrator
发表:2025-02-24 20:03:39
修改:2025-02-26 17:00:55

配置镜像

修改C:/Users/YourName/.gradle目录内的文件init.gradle(如果没有则自行创建)

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
        def ALIYUN_JCENTER_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
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            allowInsecureProtocol = true
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}
// 修改插件仓库配置
settingsEvaluated { settings ->
    settings.pluginManagement {
        repositories {
            def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin'
            def ALIYUN_SPRING_PLUGIN_URL = 'https://maven.aliyun.com/repository/spring-plugin'
            def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google'
            def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public'
            
            all { ArtifactRepository repo ->
                if(repo instanceof MavenArtifactRepository){
                    def url = repo.url.toString()
                }
            }
            
            maven { url ALIYUN_GRADLE_PLUGIN_URL }
            maven { url ALIYUN_SPRING_PLUGIN_URL }
            maven { url ALIYUN_GOOGLE_URL }
            maven { url ALIYUN_JCENTER_URL }
            
            gradlePluginPortal() // 保留这个,但放在最后作为备用
        }
    }
}

使用全局代理

作为一个合格的程序员,应该时常翻墙,常开代理,使用代理几乎时最简单的方式之一了,但是当梯子被撤下的时候可能报错

修改C:/Users/YourName/.gradle目录内的文件gradle.properties(如果没有则自行创建)

systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7890
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7890

这里我使用的是Clash,因此时代理到本机的7890端口,需要根据情况途欢对应的主机和端口

评论