java - Gradle 下载不需要的依赖项

标签 java gradle maven-central

我正在尝试使用 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/
  • 中的缓存
  • 使用 Intellij IDEA 使缓存无效
  • 删除我的本地 maven

  • 但这个问题仍然存在。

    我将 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/

    相关文章:

    java - Java Servlet 中的 UTF-8 在部署到 Azure 网站时不起作用

    Android Gradle 下载库不完整

    ionic-framework - Ionic App - 构建失败,出现 "Minimum supported Gradle version is 4.4"

    java - Maven 依赖项失败并出现 501 错误

    java - 拦截有返回值的接口(interface)默认方法

    java - 将 Java while 循环转换为 Stream 以提高性能

    java - 什么时候消息传递(例如 JMS)是多线程的替代方案?

    android - 无法解决::tslocationmanager:

    android - 将自己的库从私有(private)存储库发布到 maven Central

    android - 通过 BinTray 部署 AAR 到 MavenCentral