Skip to content

问题描述

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

    buildscript {
        repositories {
            mavenLocal()
            maven { ArtifactRepository repo ->
                // 判断属性是否存在,之后再设置
                if (repo.metaClass.hasProperty(repo, 'allowInsecureProtocol')) {
                    allowInsecureProtocol true
                }
                url REPOSITORY_URL
            }
            mavenCentral()
        }
    }
}