如何在init.gradle中配置dependencyResolutionManagement?
问题描述
尝试安装软件包时,Gradle始终访问处于故障状态的https://jitpack.io/。已配置包含所需镜像的私有代理仓库,但由于依赖使用2.19.+这类带通配符的版本号,Gradle仍会解析到Jitpack查找版本元数据。
当前$HOME/.gradle/init.gradle内容
cat <<EOT > $HOME/.gradle/init.gradle allprojects { configurations.all { resolutionStrategy { force "com.facebook.react:react-native:0.70.5" } } repositories { mavenLocal() maven { url = uri('https://mine/') credentials { ... } } <-- tried putting here } <-- and here
尝试过的配置
曾尝试添加以下代码块,但系统提示“signature not found”:
dependencyResolutionManagement { repositories { maven { url = uri('https://mine/') credentials { ... } } } }
之后参考相关问题的代码,依然无效:
settingsEvaluated { it.dependencyResolutionManagement { repositories { maven { url = uri('https://mine/') credentials { ... } } } } }
最终错误信息
[RUN_GRADLEW] A problem occurred configuring project ':rn-pendo-sdk'. [RUN_GRADLEW] > Could not resolve all files for configuration ':rn-pendo-sdk:implementation'. [RUN_GRADLEW] > Could not resolve sdk.pendo.io:pendoIO:2.19.+. [RUN_GRADLEW] Required by: [RUN_GRADLEW] project :rn-pendo-sdk [RUN_GRADLEW] > Failed to list versions for sdk.pendo.io:pendoIO. [RUN_GRADLEW] > Unable to load Maven meta-data from https://www.jitpack.io/sdk/pendo/io/pendoIO/maven-metadata.xml. [RUN_GRADLEW] > Could not GET 'https://www.jitpack.io/sdk/pendo/io/pendoIO/maven-metadata.xml'. Received status code 521 from server:
解决方案
1. 强制指定具体版本+仅保留私有仓库
带通配符的版本号会触发Gradle拉取版本元数据,必须指定具体版本,同时移除所有可能指向Jitpack的仓库:
allprojects { configurations.all { resolutionStrategy { // 替换成私有仓库中存在的pendoIO具体版本 force "sdk.pendo.io:pendoIO:2.19.0" force "com.facebook.react:react-native:0.70.5" } } repositories { mavenLocal() // 仅保留私有仓库,移除mavenCentral、jitpack等其他仓库 maven { url = uri('https://mine/') credentials { // 你的私有仓库凭证 } } } }
2. 保留其他仓库但排除Jitpack
如果需要保留其他可用仓库,可通过仓库过滤器禁止Jitpack的访问:
allprojects { repositories { mavenLocal() // 私有仓库优先级最高 maven { url = uri('https://mine/') credentials { // 你的私有仓库凭证 } } // 按需添加其他可用仓库,比如mavenCentral() // mavenCentral() } configurations.all { resolutionStrategy { force "sdk.pendo.io:pendoIO:2.19.0" force "com.facebook.react:react-native:0.70.5" // 过滤掉Jitpack仓库 repositoryFilter { details -> details.repository.name != "jitpack" } } } }
3. 验证私有仓库元数据
确认私有代理仓库已正确缓存sdk.pendo.io:pendoIO的maven-metadata.xml文件及对应版本包。若元数据缺失,Gradle仍会尝试去其他仓库查找。
内容的提问来源于stack exchange,提问作者Archimedes Trajano
相关产品推荐
相关产品推荐

