Last active
August 19, 2024 07:22
-
-
Save AlexV525/5aff2a1621482e11abc8e97d88200ac1 to your computer and use it in GitHub Desktop.
Mirroring Gradle repositories
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
// Thanks to: https://gist.github.com/bennyhuo/af7c43cc4831661193605e124f539942 | |
val urlMappings = mapOf( | |
"https://dl.google.com/dl/android/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/", | |
"https://repo.maven.apache.org/maven2" to "https://mirrors.tencent.com/nexus/repository/maven-public/", | |
"https://plugins.gradle.org/m2" to "https://mirrors.tencent.com/nexus/repository/gradle-plugins/" | |
) | |
fun RepositoryHandler.mirroring() { | |
all { | |
if (this is MavenArtifactRepository) { | |
val originalUrl = this.url.toString().removeSuffix("/") | |
urlMappings[originalUrl]?.let { | |
logger.lifecycle("Mirroring [$url] to [$it]") | |
this.setUrl(it) | |
} | |
} | |
} | |
} | |
gradle.allprojects { | |
buildscript { | |
repositories.mirroring() | |
} | |
repositories.mirroring() | |
} | |
gradle.beforeSettings { | |
pluginManagement.repositories.mirroring() | |
if (gradle.gradleVersion >= "6.8") { | |
val getDrm = settings.javaClass.getDeclaredMethod("getDependencyResolutionManagement") | |
val drm = getDrm.invoke(settings) | |
val getRepos = drm.javaClass.getDeclaredMethod("getRepositories") | |
val repos = getRepos.invoke(drm) as RepositoryHandler | |
repos.mirroring() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment