Gradle 修改私有或国内 Maven 仓库地址

发布时间: 更新时间: 总字数:502 阅读时间:2m 作者: IP上海 分享 网址

在国内使用maven编译java很慢,如果能切换到国内的Maven镜像仓库,如阿里云的Maven库,又或者是换成自建的Maven私服,那想必是极好的。

简单配置方式

修改项目根目录下的 build.gradle,将 jcenter() 或者 mavenCentral() 替换掉即可:

allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}

高级配置方式

$USER_HOME/.gradle/ 文件夹下,创建文件 init.gradle,内容如下:

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        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."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

init.gradle文件其实是Gradle的初始化脚本(Initialization Scripts),也是运行时的全局配置。

阿里云更多 repo 仓库:http://maven.aliyun.com/nexus/content/repositories/

Index of /repositories
Name    Last Modified   Size    Description
Parent Directory
Central-snapshot/   Sat Apr 07 11:53:26 CST 2018
apache-snapshots/   Sat Apr 07 11:53:26 CST 2018
central-m1/ Sat Apr 07 11:53:26 CST 2018
central/    Sat Apr 07 11:53:26 CST 2018
google-android/ Sat Apr 07 11:53:26 CST 2018
google/ Sat Apr 07 11:53:26 CST 2018
gradle-plugin/  Sat Apr 07 11:53:26 CST 2018
hongkong-nexus/ Sat Apr 07 11:53:26 CST 2018
jcenter/    Sat Apr 07 11:53:26 CST 2018
public/ Sat Apr 07 11:53:26 CST 2018
releases/   Sat Apr 07 11:53:26 CST 2018
snapshots/  Sat Apr 07 11:53:26 CST 2018
spring-plugin/  Sat Apr 07 11:53:26 CST 2018
spring/ Sat Apr 07 11:53:26 CST 2018
thirdparty/ Sat Apr 07 11:53:26 CST 2018

mavenCentral镜像仓库地址

http://maven.aliyun.com/nexus/content/groups/public/

jentral镜像仓库地址

http://maven.aliyun.com/nexus/content/repositories/jcenter

google镜像仓库地址

http://maven.aliyun.com/nexus/content/repositories/google

参考

  1. http://gradle.org/docs/current/userguide/init_scripts.html
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数