Gradle編譯時在本地倉庫中如果沒有發現依賴,就會從遠程倉庫中下載,
默認的遠程倉庫為 mavenCentral(),即 http://repo1.maven.org/maven2/往往訪問速度特別慢,經常會下載超時或者需要很長時間導致Build失敗,
因此,可以用如下的國內倉庫代替:
- 阿里的倉庫地址:http://maven.aliyun.com/nexus/content/groups/public/
- OSChina的倉庫地址:http://maven.oschina.net/content/groups/public/
阿里雲的速度比較快,更穩定一些。
一、如果使用gradle,修改gradle配置
1、修改單獨項目
修改 build.gradle文件,修改repositories配置
由:
repositories {
mavenCentral()
}
改為:
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
或:
repositories {
maven{ url 'http://maven.oschina.net/content/groups/public/'}
}
這樣就可以從國內的鏡像中下載依賴,速度能提高不少!!!
注意:
build.gradle文件里有兩處repositories,都需要改掉
2、修改所有項目
如果想一次更改所有的倉庫地址,可以在USER_HOME/.gradle/(如C:\Users\hellowood\.gradle)文件夾下添加init.gradle文件來配置
init.gradle
allprojects{
repositories {
def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/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/')) {
remove repo
}
}
}
maven {
url REPOSITORY_URL
}
}
}
這樣就可以在項目編譯的時候從阿里的倉庫中下載依賴了
init.build腳本可以參考https://docs.gradle.org/current/userguide/init_scripts.html
二、如果使用maven,修改maven配置
阿里雲搭建了一個國內鏡像 http://maven.aliyun.com/,速度很快,在Maven的settings.xml配置, 只需在mirrors節點里面加上一個mirror子節點,
內容如下:
<mirror> <!--This sends everything else to /public --> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
修改完畢之后,就可以體驗下飛快的感覺了,如果進度條還是很慢,嘗試重啟下 IDE 。