使用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/") 
         }
         //...
    }
    


免責聲明!

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



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