Gradle7.1忽略https检查

问题描述

Gradle7下使用http私库时,提示要使用 https, 可以使用 allowInsecureProtocol true 关闭。

Could not resolve all dependencies for configuration ':compileClasspath'.
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://127.0.0.1:8081/repository/maven-public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.1/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

但是该属性在旧版Gradle(例如5.6.2)中不存在,会提示错误;

解决方式

init.gradle 文件位置: C:\Users\[用户名]\.gradle

init.gradle中,检查 ArtifactRepository 对象中是否存在 allowInsecureProtocol属性,如果存在才设置,否则忽略;最终 init.gradle 配置文件如下。

allprojects {
    repositories {
        def REPOSITORY_URL = 'http://127.0.0.1:8081/repository/maven-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 { ArtifactRepository repo -> 
            // 判断属性是否存在,之后再设置
            if(repo.metaClass.hasProperty(repo, 'allowInsecureProtocol')){
                allowInsecureProtocol true
            }
            url REPOSITORY_URL
        }
    }
}

你可能感兴趣的:(Gradle7.1忽略https检查)