-
-
Save twiceyuan/a866fa0ea0d0e84ae9ed1ba9f3e169f0 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?
This file contains hidden or 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
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