配置Gradle使用GitHub Packages私有插件遇依赖缺失,如何解决?
问题解决:使用GitHub Packages私有Gradle插件时的依赖缺失问题
问题背景
已将自定义Gradle插件(io.github.benkeil.openapi-dsl-gradle)发布至GitHub Packages或mavenLocal,消费项目中配置了pluginManagement和仓库,但构建时出现以下错误:
> Could not resolve all files for configuration ':classpath'. > Could not find org.gradle:gradle-tooling-api:8.9. Searched in the following locations: - https://plugins.gradle.org/m2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom - https://dl.google.com/dl/android/maven2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom - file:/Users/ben/.m2/repository/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom - https://repo.maven.apache.org/maven2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom Required by: project : > io.github.benkeil:openapi-dsl-gradle:main
错误原因
- 缺失Gradle官方依赖仓库:
gradle-tooling-api属于Gradle官方专属依赖,仅在Gradle私有仓库分发,当前pluginManagement未配置该仓库,导致无法拉取。 - GitHub Packages仓库配置错误:消费项目中GitHub Packages的URL使用了通配符
*,不符合仓库地址规范;同时pluginManagement未配置GitHub Packages仓库,插件本身的拉取路径不全。 - 插件版本不规范:报错中使用的插件版本为分支名
main,属于非标准版本号,易引发依赖解析异常。
解决方案
1. 修正Plugin Management配置(settings.gradle.kts)
在pluginManagement中添加Gradle官方仓库和GitHub Packages仓库,确保插件及其依赖能被正确解析:
rootProject.name = "cdktf-lib" pluginManagement { repositories { gradlePluginPortal() // 添加Gradle官方依赖仓库,用于拉取gradle-tooling-api等核心依赖 maven { url = uri("https://repo.gradle.org/gradle/libs-releases-local/") } google() mavenLocal() mavenCentral() // 添加GitHub Packages仓库,用于拉取私有插件 maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/benkeil/openapi-dsl-gradle") credentials { username = "not_required" password = System.getenv("GITHUB_PACKAGES_READ_TOKEN") } } } resolutionStrategy { eachPlugin { if (requested.id.namespace == "io.github.benkeil") { useModule("${requested.id.namespace}:${requested.id.name}:${requested.version}") } } } }
2. 修正消费项目的仓库配置(build.gradle.kts)
移除GitHub Packages URL中的通配符,确保地址准确:
repositories { mavenLocal() mavenCentral() maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/benkeil/openapi-dsl-gradle") credentials { username = "not_required" password = System.getenv("GITHUB_PACKAGES_READ_TOKEN") } content { includeGroupByRegex("de\\.benkeil.*") includeGroupByRegex("pub\\.keil.*") includeGroupByRegex("io\\.github\\.benkeil.*") } } } dependencies { implementation(kotlin("reflect")) implementation(libs.bundles.implementation) testImplementation(libs.bundles.testImplementation) }
3. 使用规范的插件版本
确保消费项目中使用的插件版本与发布时的版本一致。例如,如果插件发布时生成了1.0.0-SNAPSHOT版本,消费时需明确指定:
// 在消费项目的plugins块中指定正确版本 plugins { id("io.github.benkeil.openapi-dsl-gradle") version "1.0.0-SNAPSHOT" }
4. 本地发布验证(可选)
如果使用mavenLocal测试,确认插件已正确发布到本地仓库,且消费项目的pluginManagement和repositories中包含mavenLocal(),优先从本地拉取依赖。
内容的提问来源于stack exchange,提问作者Ben Keil
相关产品推荐
相关产品推荐

