Skip to content

Instantly share code, notes, and snippets.

@bennyhuo
Last active June 27, 2024 03:12
Show Gist options
  • Save bennyhuo/af7c43cc4831661193605e124f539942 to your computer and use it in GitHub Desktop.
Save bennyhuo/af7c43cc4831661193605e124f539942 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()
}
@Jacknic
Copy link

Jacknic commented Jul 3, 2023

image

Gradle 低于6.8 没有 dependencyResolutionManagement 相关的 API,旧项目有兼容问题。
注释了好像影响不大🙊

@bennyhuo
Copy link
Author

bennyhuo commented Jul 3, 2023

image

Gradle 低于6.8 没有 dependencyResolutionManagement 相关的 API,旧项目有兼容问题。 注释了好像影响不大🙊

对,这个 API 出现的比较晚,现在新版本比较推荐使用这个 api 直接配置整个项目的工程依赖,如果没有用到也确实不会有什么实质影响。当然,理论上也可以考虑加个版本控制。

@Jacknic
Copy link

Jacknic commented Jul 9, 2023

Gradle 旧版本兼容问题,我这边通过版本判断及反射读取相关字段,配置 dependencyResolutionManagement ,这样应该也可以,自己试了是生效的。

参考链接

gradle.beforeSettings {
    pluginManagement.repositories.enableMirror()
    // 6.8 及更高版本执行 DependencyResolutionManagement 配置
    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.enableMirror()
        println("Gradle ${gradle.gradleVersion} DependencyResolutionManagement Configured $settings")
    } else {
        println("Gradle ${gradle.gradleVersion} DependencyResolutionManagement Ignored $settings")
    }
}

运行结果

6.8 及以上

Gradle 8.0 DependencyResolutionManagement Configured settings 'Jetpack'
Repository Google [https://dl.google.com/dl/android/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
Repository MavenRepo [https://repo.maven.apache.org/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
Repository Gradle Central Plugin Repository [https://plugins.gradle.org/m2] is mirrored to https://mirrors.tencent.com/nexus/repository/gradle-plugins/
Repository Google [https://dl.google.com/dl/android/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
Repository MavenRepo [https://repo.maven.apache.org/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
...

6.8 以下

Gradle 6.1.1 DependencyResolutionManagement Ignored settings 'DemoApp'

> Configure project :
Repository Google [https://dl.google.com/dl/android/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
Repository MavenRepo [https://repo.maven.apache.org/maven2/] is mirrored to https://mirrors.tencent.com/nexus/repository/maven-public/
Repository maven6 [https://plugins.gradle.org/m2/] is mirrored to https://mirrors.tencent.com/nexus/repository/gradle-plugins/
...

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