Skip to content

Instantly share code, notes, and snippets.

@qbosen
Forked from bennyhuo/init.gradle.kts
Created July 5, 2023 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qbosen/2031de14be0df2108f03843b307f4107 to your computer and use it in GitHub Desktop.
Save qbosen/2031de14be0df2108f03843b307f4107 to your computer and use it in GitHub Desktop.
How to config mirrors for repositories in Gradle without changing the source code of your project?
fun RepositoryHandler.enableMirror() {
all {
if (this is MavenArtifactRepository) {
val originalUrl = this.url.toString().removeSuffix("/")
urlMappings[originalUrl]?.let {
logger.lifecycle("Repository[$url] is mirrored to $it")
this.setUrl(it)
}
}
}
}
val urlMappings = mapOf(
"https://repo.maven.apache.org/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://dl.google.com/dl/android/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/",
"https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/"
)
gradle.allprojects {
buildscript {
repositories.enableMirror()
}
repositories.enableMirror()
}
gradle.beforeSettings {
pluginManagement.repositories.enableMirror()
dependencyResolutionManagement.repositories.enableMirror()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment