1. 程式人生 > >配置Gradle的映象為阿里雲映象

配置Gradle的映象為阿里雲映象

預設的Gradle的映象下載jar包會比較慢,那麼就需要更換為國內的源,目前可用的源是阿里雲提供的,只需要如下配置即可:

repositories {
    // maven庫
    def cn = "http://maven.aliyun.com/nexus/content/groups/public/"
    def abroad = "http://central.maven.org/maven2/"
    // 先從url中下載jar若沒有找到,則在artifactUrls中尋找
    maven {
        url cn
        artifactUrls abroad
    }
}

高階配置:
在~/.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),也是執行時的全域性配置。