Skip to content

Instantly share code, notes, and snippets.

@mkckr0
Last active April 24, 2024 07:57
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mkckr0/97ec5b0d99feede4c19ee6f905d5e722 to your computer and use it in GitHub Desktop.
Save mkckr0/97ec5b0d99feede4c19ee6f905d5e722 to your computer and use it in GitHub Desktop.
init.gradle.kts
// 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()
}
}
@mkckr0
Copy link
Author

mkckr0 commented Sep 18, 2023

Gradle 设置全局镜像源,支持 Android 项目,解决镜像源缺失 POM 或 JAR 文件,支持直接替换 URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment