問題描述
Gradle7下使用http私庫(kù)時(shí),提示要使用 https, 可以使用 allowInsecureProtocol true 關(guān)閉。
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)中不存在,會(huì)提示錯(cuò)誤;
解決方式
init.gradle 文件位置: C:\Users\[用戶名]\.gradle
在init.gradle中,檢查 ArtifactRepository 對(duì)象中是否存在 allowInsecureProtocol屬性,如果存在才設(shè)置,否則忽略;最終 init.gradle 配置文件如下。
allprojects {
def REPOSITORY_URL = 'http://127.0.0.1:8081/repository/maven-public/'
repositories {
mavenLocal()
maven { ArtifactRepository repo ->
// 判斷屬性是否存在,之后再設(shè)置
if (repo.metaClass.hasProperty(repo, 'allowInsecureProtocol')) {
allowInsecureProtocol true
}
url REPOSITORY_URL
}
mavenCentral()
}
buildscript {
repositories {
mavenLocal()
maven { ArtifactRepository repo ->
// 判斷屬性是否存在,之后再設(shè)置
if (repo.metaClass.hasProperty(repo, 'allowInsecureProtocol')) {
allowInsecureProtocol true
}
url REPOSITORY_URL
}
mavenCentral()
}
}
}