修改 Gradle 插件(Plugins)的下載地址(repositories)


Gradle 也可以用下面的方式聲明使用的插件:

1
2
3
4
// build.gradle
plugins {
id 'com.example.plugin', version '1.0'
}

其實是從 Gradle 官方的插件倉庫 https://plugins.gradle.org/m2/ 下載的。

但是,眾所周知的原因,某些地區會連不上,導致下載不到需要的插件,例如出現如下錯誤:

1
2
3
4
5
6
7
* What went wrong:
A problem occurred configuring root project 'MyApp'.
> Could not resolve all files for configuration ':classpath'.
> Could not download jimfs.jar (com.google.jimfs:jimfs:1.1)
> Could not get resource 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
> Could not HEAD 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
> Connect to d29vzk4ow07wi7.cloudfront.net:443 [d29vzk4ow07wi7.cloudfront.net/54.192.84.6, d29vzk4ow07wi7.cloudfront.net/54.192.84.168, d29vzk4ow07wi7.cloudfront.net/54.192.84.128, d29vzk4ow07wi7.cloudfront.net/54.192.84.173] failed: Read timed out

又或者,插件是不對外的,存在某個私有倉庫的,該如何修改或者添加額外的私有倉庫地址呢?

直接修改根目錄(root project)的settings.gradle來添加其它倉庫:

1
2
3
4
5
6
7
8
9
// settings.gradle
pluginManagement {
repositories {
maven {
url 'http://examle.com/maven-repo'
}
gradlePluginPortal()
}
}

比如使用阿里雲提供的鏡像

1
2
3
maven {
url 'https://maven.aliyun.com/repository/gradle-plugin'
}

當然你也可以使用老的方式應用插件:

1
2
3
4
5
6
7
8
9
10
11
12
buildscript {
repositories {
maven {
url 'http://examle.com/maven-repo'
}
}
dependencies {
classpath 'com.example.plugin:gradle-plugin:1.0'
}
}

apply plugin: 'com.example.plugin'

references:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM