Skip to content

Instantly share code, notes, and snippets.

@chisaato
Last active March 15, 2023 16:33
Show Gist options
  • Save chisaato/2d28f626c3d6f82b0ca404c424838dca to your computer and use it in GitHub Desktop.
Save chisaato/2d28f626c3d6f82b0ca404c424838dca to your computer and use it in GitHub Desktop.
全国首发,更换阿里云镜像的kts脚本
import kotlin.*
import kotlin.reflect.full.*
var replaceRepository = { arg: RepositoryHandler ->
var ALIYUN_MAVEN_REPOSITORY_URL = "https://maven.aliyun.com/repository/central"
// var ALIYUN_MAVEN_REPOSITORY_URL = "https://mirrors.cloud.tencent.com/nexus/repository/maven-public"
var ALIYUN_JCENTER_URL = "https://maven.aliyun.com/repository/jcenter"
var ALIYUN_GOOGLE_URL = "https://maven.aliyun.com/repository/google"
var ALIYUN_GRADLE_PLUGIN_URL = "https://maven.aliyun.com/repository/gradle-plugin"
var ALIYUN_SPRING_URL = "https://maven.aliyun.com/repository/spring"
var ALIYUN_SPRING_PLUGIN_URL = "https://maven.aliyun.com/repository/spring-plugin"
var ALIYUN_GRAILS_CORE_URL = "https://maven.aliyun.com/repository/grails-core"
var ALIYUN_APACHE_SNAPSHOTS_URL = "https://maven.aliyun.com/repository/apache-snapshots"
var MINECRAFT_MAVEN_CN_URL = "https://lss233.littleservice.cn/repositories/minecraft"
var repos = arg as RepositoryHandler
// 首先确定我们在哪里
var stackTraces = Thread.currentThread().getStackTrace()
var hasBuildScript = false
for (st in stackTraces) {
if (st.className.contains("project.BuildScript")) {
hasBuildScript = true
}
}
// 从这里开始下面都不再打注释
// 貌似打注释行为会导致一些东西出错
if (hasBuildScript) {
// println("🔨 当前位于 buildscript 块")
} else {
// println("🧲 当前位于项目依赖块")
}
// 修改前 列出所有的库
// println("列出所有库 (修改前)")
// repos.all {
// if (this is MavenArtifactRepository) {
// println("Maven库(修改前): ${this.url.toString()}")
// }
// }
// println(repos.javaClass.kotlin)
// 实际修改部分
repos.all {
if (this is MavenArtifactRepository) {
var oldUrl = this.url.toString()
// 改用contains判断
if (oldUrl.contains("repo.maven.apache.org/maven2")) {
this.url = uri(ALIYUN_MAVEN_REPOSITORY_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_MAVEN_REPOSITORY_URL}")
}
if (oldUrl.contains("jcenter.bintray.com")) {
this.url = uri(ALIYUN_JCENTER_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_JCENTER_URL}")
}
if (oldUrl.contains("maven.google.com")) {
this.url = uri(ALIYUN_GOOGLE_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_GOOGLE_URL}")
}
if (oldUrl.contains("plugins.gradle.org/m2")) {
this.url = uri(ALIYUN_GRADLE_PLUGIN_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_GRADLE_PLUGIN_URL}")
}
if (oldUrl.contains("repo.spring.io/libs-milestone")) {
this.url = uri(ALIYUN_SPRING_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_SPRING_URL}")
}
if (oldUrl.contains("repo.spring.io/plugins-release")) {
this.url = uri(ALIYUN_SPRING_PLUGIN_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_SPRING_PLUGIN_URL}")
}
if (oldUrl.contains("repository.apache.org/snapshots/")) {
this.url = uri(ALIYUN_APACHE_SNAPSHOTS_URL)
// println("仓库 ${oldUrl} 已经替换为 ${ALIYUN_APACHE_SNAPSHOTS_URL}")
}
if (oldUrl.contains("maven.minecraftforge.net")) {
// this.url = uri(MINECRAFT_MAVEN_CN_URL)
// println("[Minecraft] Forge 仓库已经替换为 ${MINECRAFT_MAVEN_CN_URL}")
}
// println(this.url.toString())
}
}
// 自动添加本地仓库,但是实现有问题
// if (!repos.contains(repos.mavenLocal())) {
// println("添加maven本地仓库")
// this.add(repos.mavenLocal())
// }
// 修改后 列出所有的库
// println("列出所有库 (修改后)")
repos.all {
if (this is MavenArtifactRepository) {
// println("Maven库(修改后): ${this.url.toString()}")
}
}
// 反射实验
// repo.all {
// if (this is MavenArtifactRepository) {
// println("Maven库: ${this.url.toString()}")
// }
// println("这是非maven库: $this")
// println("这是非maven库的地址: ${this.getUrl()}")
// println("这些非maven库具有以下函数")
// var allFunc = this::class.functions
// allFunc.forEach {
// println(it.name)
// }
// println("这些非maven库具有以下字段")
// var allFields = this::class.memberProperties
// allFields.forEach {
// println(it.name)
// }
// }
}
allprojects {
buildscript {
repositories(replaceRepository)
}
repositories(replaceRepository)
}
@chisaato
Copy link
Author

阿里云和腾讯云镜像都可以,主要是阿里有时候不太好使

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