Skip to content

Instantly share code, notes, and snippets.

@diguage
Last active July 20, 2021 05:54
Show Gist options
  • Save diguage/56894cc2861814bf58416c640db73f18 to your computer and use it in GitHub Desktop.
Save diguage/56894cc2861814bf58416c640db73f18 to your computer and use it in GitHub Desktop.
Aliyun Mirrors for Gradle
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
// https://repo1.maven.org/maven2/
private static String CENTRAL_URL = "https://maven.aliyun.com/repository/central"
// http://jcenter.bintray.com/
private static String JCENTER_URL = "https://maven.aliyun.com/repository/jcenter"
// https://maven.google.com/
private static String GOOGLE_URL = "https://maven.aliyun.com/repository/google"
// https://plugins.gradle.org/m2/
private static String GRADLE_PLUGIN_URL = "https://maven.aliyun.com/repository/gradle-plugin"
// http://repo.spring.io/libs-milestone/
private static String SPRING_URL = "https://maven.aliyun.com/repository/spring"
// http://repo.spring.io/plugins-release/
private static String SPRING_PLUGIN_URL = "https://maven.aliyun.com/repository/spring-plugin"
// https://repo.grails.org/grails/core
private static String GRAILS_CORE_URL = "https://maven.aliyun.com/repository/grails-core"
// https://repository.apache.org/snapshots/
private static String APACHE_SNAPSHOTS_URL = "https://maven.aliyun.com/repository/apache-snapshots"
void apply(Gradle gradle) {
// ONLY USE aliyun FOR DEPENDENCIES
gradle.allprojects { project ->
project.repositories {
// Remove all repositories not pointing to the aliyun repositories
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.contains("repo1.maven.org/maven2")) {
project.logger.lifecycle "Repository ${url} removed. Only ${CENTRAL_URL} is allowed"
remove repo
}
if (url.contains("jcenter.bintray.com")) {
project.logger.lifecycle "Repository ${url} removed. Only ${JCENTER_URL} is allowed"
remove repo
}
if (url.contains("maven.google.com")) {
project.logger.lifecycle "Repository ${url} removed. Only ${GOOGLE_URL} is allowed"
remove repo
}
if (url.contains("plugins.gradle.org/m2")) {
project.logger.lifecycle "Repository ${url} removed. Only ${GRADLE_PLUGIN_URL} is allowed"
remove repo
}
if (url.contains("repo.spring.io/libs-milestone")) {
project.logger.lifecycle "Repository ${url} removed. Only ${SPRING_URL} is allowed"
remove repo
}
if (url.contains("repo.spring.io/plugins-release")) {
project.logger.lifecycle "Repository ${url} removed. Only ${SPRING_PLUGIN_URL} is allowed"
remove repo
}
if (url.contains("repo.grails.org/grails/core")) {
project.logger.lifecycle "Repository ${url} removed. Only ${GRAILS_CORE_URL} is allowed"
remove repo
}
if (url.contains("repository.apache.org/snapshots")) {
project.logger.lifecycle "Repository ${url} removed. Only ${APACHE_SNAPSHOTS_URL} is allowed"
remove repo
}
}
}
// add the aliyun repository
maven {
name "central"
url CENTRAL_URL
}
maven {
name "jcenter"
url JCENTER_URL
}
maven {
name "google"
url GOOGLE_URL
}
maven {
name "gradlePlugin"
url GRADLE_PLUGIN_URL
}
maven {
name "spring"
url SPRING_URL
}
maven {
name "springPlugin"
url SPRING_PLUGIN_URL
}
maven {
name "grailsCore"
url GRAILS_CORE_URL
}
maven {
name "apacheSnapshots"
url APACHE_SNAPSHOTS_URL
}
mavenLocal()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment