我正在尝试使用 Gradle 从 Maven Central 下载我新发布的依赖项:
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.iltotore:ec-client_2.13:1.0-fixed'
}
尝试构建时,出现错误:
Could not find io.github.iltotore:core:1.0-fixed.
Required by:
project : > io.github.iltotore:ec-client_2.13:1.0-fixed
但是 io.github.iltotore:core:1.0-fixed 不在 lib 的 pom 中,我的 friend 可以毫无错误地使用它。
为了解决这个问题,我尝试了:
gradlew build --refresh-dependency
但这个问题仍然存在。
我将 Gradle 6.5 与 Intellij IDEA 2020.1.2 一起使用。
最佳答案
从 Gradle 6.0 开始,Maven Publish 插件也将发布 Gradle Module Metadata .元数据的文件扩展名为 .module
您可以在存储库中看到它here .
如果您打开 pom file ,您会注意到顶部有一条评论说:
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
这指示 Gradle 使用元数据文件而不是 pom 文件。
如果您打开 metadata file ,你可以看到它确实依赖于不存在的模块:
"dependencies": [
...
{
"group": "io.github.iltotore",
"module": "core",
"version": {
"requires": "1.0-fixed"
}
}
]
由于
core
在任何版本中都不存在,我认为这是一个错误。也许您正在自定义 pom 文件,但没有为模块元数据执行此操作。有几种方法可以解决这个问题(以下所有代码片段都适用于 Groovy DSL)。
A. 发布没有模块元数据的新版本
这样你就完全依赖 pom 文件。您可以通过以下方式做到这一点:
tasks.withType(GenerateModuleMetadata) {
enabled = false
}
见 Understanding Gradle Module Metadata在 Gradle 用户指南中。
B. 在消费项目中禁用存储库中的模块元数据
请注意,这对所有模块都有效,而不仅仅是损坏的模块:
repositories {
mavenCentral {
metadataSources {
mavenPom()
artifact()
ignoreGradleMetadataRedirection()
}
}
}
见 Declaring repositories在 Gradle 用户指南中。
C. 更正此特定依赖项的消费端元数据
就像是:
dependencies {
dependencies {
components {
// Fix wrong dependency in client_2.13
withModule("io.github.iltotore:ec-client_2.13") {
allVariants {
withDependencies {
removeAll { it.group == "io.github.iltotore" && it.name == "core" }
}
}
}
}
}
implementation 'io.github.iltotore:ec-client_2.13:1.0-fixed'
}
见 Fixing metadata with component metadata rules在 Gradle 用户指南中。
关于java - Gradle 下载不需要的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62464873/