本文主要是介绍Gradle在国内配置镜像加速的实现步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Gradle在国内配置镜像加速的实现步骤》在国内使用Gradle构建项目时,最大的痛点就是依赖下载贼慢,甚至卡死,下面教你如何配置国内镜像加速Gradle下载依赖,主要是通过改写repositori...
引言
在国内使用 Gradle 构建项目时,最大的痛点就是 依赖下载贼慢,甚至卡死。下面教你如何 配置国内镜像加速 Gradle 下载依赖,主要是通过改写 repositories 中的源地址来实现。亲测有效。
一、修改 build.gradle 或 settings.gradle 的 repositories 配置
把默认的 jcenter()、mavenCentral() 或 google() 替换成国内镜像,比如阿里云或清华。
示例:使用阿里云加速
repositories {
maven { url 'https://maven.aliyun.com/repository/public' } js // Maven Central
maven { url 'https://maven.aliyun.com/repository/jcenter' } // JCenter
maven { url 'https://maven.aliyun.com/repository/google' } // Google
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } // Gradle 插件
}或者使用清华镜像(也比较稳定)
repositories {
maven { url 'https://mirrors.tuna.tsinghua.edu.cn/maven/central/' }
maven { url 'https://mirrors.tuna.tsinghua.edu.cn/gradle/' }
}二、修改全局配置(推荐用于多项目)
如果你不想每个项目都写一次,可以配置到全局:
路径:
内容如下(自动全局替换 Maven 仓库为阿里云):
allprojects {
repositories {
all { ArtifactRepository repo ->
if (repo instanChina编程ceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.contains('jcenter.bintray.com')) {
repo.setUrl('https://maven.aliyun.com/repository/jcenter')
}
if (url.contains('mavenCentral')) {
编程China编程 repo.setUrl('https://maven.aliyun.com/repository/central')
}
if (url.contains('plugins.gradle.org')) {
repo.setUrl('https://maven.aliyun.com/repository/gradle-plugin')
}
if (url.contains('dl.google.com')) {
repo.setUrl('https://maven.aliyun.com/repository/google')
}
}
}
}
}这样就能实现“偷偷地换源”,你原来 build.gradle 里啥都不用动。
三、代理加速(配合魔法使用)
在 ~/.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
你也可以使用环境变量配置:
export GRADLE_OPTS="-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7890 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7890"
检查是否生效
执行构建命令并加上 --info,例如:
./gradlew build --info
可以看到使用了哪个仓库 URL,如果是 aliyunwww.chinasem.cn.com 或 tuna.tsinghua.edu.cn 就说明成功了。
到此这篇关于Gradle在国内配置镜像加速的实现步骤的文章就介绍到这了,China编程更多相关Gradle国内配置镜像加速内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!
这篇关于Gradle在国内配置镜像加速的实现步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!