Gradle 使用阿里云国内镜像

  1. 对单个项目生效,在项目中的build.gradle修改内容
buildscript {
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
    }
}

allprojects {
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
    }
}

  1. 对所有项目生效,在USER_HOME/.gradle/下创建init.gradle文件
allprojects {
    repositories {
        def ALIYUN_REPOSITORY_URL_PUBLIC = 'https://maven.aliyun.com/repository/public'
        def ALIYUN_REPOSITORY_URL_GOOGLE = 'https://maven.aliyun.com/repository/google'
        def ALIYUN_REPOSITORY_URL_GRADLE_PLUGIN = 'https://maven.aliyun.com/repository/gradle-plugin'
        def ALIYUN_REPOSITORY_URL_SPRING = 'https://maven.aliyun.com/repository/spring'
        def ALIYUN_REPOSITORY_URL_SPRING_PLUGIN = 'https://maven.aliyun.com/repository/spring-plugin'
        def ALIYUN_REPOSITORY_URL_GRAILS_CORE = 'https://maven.aliyun.com/repository/grails-core'
        def ALIYUN_REPOSITORY_URL_APACHE_SNAPSHOTS = 'https://maven.aliyun.com/repository/apache-snapshots'
        all {
            ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_PUBLIC."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_PUBLIC."
                        remove repo
                    }
                    if (url.startsWith('https://maven.google.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_GOOGLE."
                        remove repo
                    }
                    if (url.startsWith('https://plugins.gradle.org/m2/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_GRADLE_PLUGIN."
                        remove repo
                    }
                    if (url.startsWith('http://repo.spring.io/libs-milestone/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_SPRING."
                        remove repo
                    }
                    if (url.startsWith('http://repo.spring.io/plugins-release/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_SPRING_PLUGIN."
                        remove repo
                    }
                    if (url.startsWith('https://repo.grails.org/grails/core')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_GRAILS_CORE."
                        remove repo
                    }
                    if (url.startsWith('https://repository.apache.org/snapshots/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL_APACHE_SNAPSHOTS."
                        remove repo
                    }
                    
                }
        }
        maven {
            url ALIYUN_REPOSITORY_URL_PUBLIC
            url ALIYUN_REPOSITORY_URL_GOOGLE
            url ALIYUN_REPOSITORY_URL_GRADLE_PLUGIN
            url ALIYUN_REPOSITORY_URL_SPRING
            url ALIYUN_REPOSITORY_URL_SPRING_PLUGIN
            url ALIYUN_REPOSITORY_URL_GRAILS_CORE
            url ALIYUN_REPOSITORY_URL_APACHE_SNAPSHOTS
        }
    }
}

Maven 使用阿里云国内镜像

1、修改maven根目录下的conf文件夹中的setting.xml文件,内容如下:

<mirrors>
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/repository/public</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

Q.E.D.


愿永远保持对技术的热情