Last active
April 24, 2024 07:57
-
-
Save mkckr0/97ec5b0d99feede4c19ee6f905d5e722 to your computer and use it in GitHub Desktop.
init.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/mkckr0/97ec5b0d99feede4c19ee6f905d5e722 | |
val repoMirrorMap = mapOf( | |
"https://repo.maven.apache.org/maven2" to "https://maven.aliyun.com/repository/central", | |
"https://dl.google.com/dl/android/maven2" to "https://maven.aliyun.com/repository/google", | |
"https://plugins.gradle.org/m2" to "https://maven.aliyun.com/repository/gradle-plugin", | |
"https://jcenter.bintray.com" to "https://maven.aliyun.com/repository/jcenter", | |
) | |
val repoReplaceMap = mapOf( | |
"https://maven.google.com" to "https://dl.google.com/dl/android/maven2" | |
) | |
fun RepositoryHandler.setMirrors() { | |
all { | |
if (this is MavenArtifactRepository && !name.endsWith("Origin")) { | |
val originName = name | |
var originUrl = url.toString().trimEnd('/') | |
// do replace | |
repoReplaceMap[originUrl]?.let { newUrl -> | |
originUrl = newUrl | |
setUrl(originUrl) | |
} | |
// do mirror | |
repoMirrorMap[originUrl]?.let { newUrl -> | |
// replace into mirror repo | |
setUrl(newUrl) | |
// add origin repo to find missing jars | |
artifactUrls(originUrl) | |
// keep origin repo to find missing POM | |
maven(originUrl) { name = "$originName Origin" } | |
} | |
} | |
} | |
printRepos() | |
} | |
fun RepositoryHandler.printRepos() { | |
all { | |
if (this is MavenArtifactRepository) { | |
println("Maven Repo: name=\"$name\", url=$url, artifacts=${artifactUrls}") | |
} | |
} | |
} | |
settingsEvaluated { | |
pluginManagement { | |
repositories { | |
setMirrors() | |
} | |
} | |
dependencyResolutionManagement { | |
@Suppress("UnstableApiUsage") | |
repositories { | |
setMirrors() | |
} | |
} | |
} | |
allprojects { | |
buildscript { | |
repositories { | |
setMirrors() | |
} | |
} | |
repositories { | |
setMirrors() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gradle 设置全局镜像源,支持 Android 项目,解决镜像源缺失 POM 或 JAR 文件,支持直接替换 URL