Skip to content

Instantly share code, notes, and snippets.

@xeecos
Created October 28, 2024 04:33
Show Gist options
  • Select an option

  • Save xeecos/a128ac1f34b4d5b205a3d023f67c9157 to your computer and use it in GitHub Desktop.

Select an option

Save xeecos/a128ac1f34b4d5b205a3d023f67c9157 to your computer and use it in GitHub Desktop.
全局替换gradle下载源,放到~/.gradle/下
/**
* Description: Set up mirrors for Gradle Globally
*/
val MAVEN_REPOSITORY_URL = "https://mirrors.cloud.tencent.com/nexus/repository/maven-public"
val CENTER_REPOSITORY_URL = "https://maven.aliyun.com/repository/jcenter"
val GOOGLE_REPOSITORY_URL = "https://maven.aliyun.com/repository/google"
val GRADLE_PLUGIN_REPOSITORY_URL = "https://maven.aliyun.com/repository/gradle-plugin"
gradle.settingsEvaluated {
pluginManagement {
// Print repositories collection
println("Plugins Repositories names: " + repositories.names)
// Clear repositories collection
repositories.clear()
// Add my Artifactory mirror
repositories {
maven {
name = "Aly Gradle Plugin Repo"
url = uri(GRADLE_PLUGIN_REPOSITORY_URL)
}
}
// Print repositories collection
println("Now Plugins Repositories names : " + repositories.names)
}
}
allprojects {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")|| url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(CENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
buildscript {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = this.url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")||url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(CENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
}
afterEvaluate {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last :\n $lastUsedRepos")
}
}
buildscript {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last in build script:\n $lastUsedRepos")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment