1. 程式人生 > >JCenter下載太慢?教你修改Maven倉庫地址為國內鏡像

JCenter下載太慢?教你修改Maven倉庫地址為國內鏡像

class nbsp ould artifact rac 地址 lur 文件夾 initial

http://blog.csdn.net/biezhihua/article/details/49668605

轉載自:http://www.yrom.net/blog/2015/02/07/change-gradle-maven-repo-url/

近來遷移了一些項目到Android Studio,采用Gradle構建確實比原來的Ant方便許多。但是編譯時下載依賴的網速又著實令人蛋疼不已。

如果能切換到國內的Maven鏡像倉庫,如開源中國的Maven庫,又或者是換成自建的Maven私服,那想必是極好的。

一個簡單的辦法,修改項目根目錄下的build.gradle,將jcenter()或者mavenCentral()替換掉即可:

allprojects {
    repositories {
        maven{ url ‘http://maven.oschina.net/content/groups/public/‘}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

但是架不住項目多,難不成每個都改一遍麽?
自然是有省事的辦法,將下面這段Copy到名為init.gradle文件中,並保存到USER_HOME/.gradle/文件夾下即可。

allprojects{
    repositories {
        def REPOSITORY_URL = ‘http://maven.oschina.net/content/groups/public‘
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith(‘https://repo1.maven.org/maven2‘) || url.startsWith(‘https://jcenter.bintray.com/‘)) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

init.gradle文件其實是Gradle的初始化腳本(Initialization Scripts),也是運行時的全局配置。

如果碰到如下錯誤,多嘗試幾次就好了:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project ‘fresco‘.
> Could not resolve all dependencies for configuration ‘:classpath‘.
   > Could not download httpcore.jar (org.apache.httpcomponents:httpcore:4.1)
      > Could not get resource ‘https://jcenter.bintray.com/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar‘.
         > SSL peer shut down incorrectly

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

JCenter下載太慢?教你修改Maven倉庫地址為國內鏡像