使用Gradle构建项目时的注意事项

1、仓库配置

默认是到maven中央仓库进行下载依赖,由于maven中央仓库服务器在国外,速度可想而知。所以,国内开发者通常会设置优先从国内仓库(如阿里云)下载。

你可能认为,只要这样:

  • with groovy
    repositories {
        //...
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
        //...
    }
    
  • with kotlin
    repositories {
        //...
        maven { setUrl("http://maven.aliyun.com/nexus/content/groups/public/") }
        //...
    }
    

结果:

* What went wrong:
Could not resolve all dependencies for configuration ':detachedConfiguration13'.
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://maven.aliyun.com/nexus/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.0.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

what fk ? 没办法,看一下gradle的描述:doc

Specifies whether it is acceptable to communicate with a repository over an insecure HTTP connection.

For security purposes this intentionally requires a user to opt-in to using insecure protocols on case by case basis.

Gradle intentionally does not offer a global system/gradle property that allows a universal disable of this check.

大概意思就是:gradle为了安全考虑,防止他人冒充目标服务器,并在资源中植入恶意代码...所以默认禁用使用非官方的中央仓库(包括阿里云),如果你确认信任该仓库,需要你显示声明信任它。

所以你需要这样配置:

  • with groovy
    repositories {
        //...
        maven { 
            allowInsecureProtocol = true
            url "http://maven.aliyun.com/nexus/content/groups/public/" 
        }
        //...
    }
    
  • with kotlin
    repositories {
         //...
         maven { 
             isAllowInsecureProtocol = true
             setUrl("http://maven.aliyun.com/nexus/content/groups/public/") 
         }
         //...
    }
    
posted @ 2021-06-12 10:08  LANGKYE#  阅读(14414)  评论(0编辑  收藏  举报