Maven 和 Ivy 依赖项解析在 Gradle 6.0 中失败

And*_*ert 5 ivy dependency-management gradle maven

我有一个工作项目依赖于这样的对等组件生成的 Maven 工件:

repositories {
   ivy {
       url "../cnf/local"
   }
}

configurations {
  ejbTools
}

dependencies {
  ejbTools 'test:com.ibm.ws.ejbcontainer.fat_tools:1.+'
}
Run Code Online (Sandbox Code Playgroud)

test:com.ibm.ws.ejbcontainer.fat_tools:1.+Gradle 6.0 无法解决依赖项,并显示以下错误:

> Task :com.ibm.ws.ejbcontainer.async_fat:addEJBTools FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/aguibert/dev/git/open-liberty/dev/com.ibm.ws.ejbcontainer.async_fat/build.gradle' line: 32

* What went wrong:
Execution failed for task ':com.ibm.ws.ejbcontainer.async_fat:addEJBTools'.
> Could not resolve all files for configuration ':com.ibm.ws.ejbcontainer.async_fat:ejbTools'.
   > Could not find any matches for test:com.ibm.ws.ejbcontainer.fat_tools:1.+ as no versions of test:com.ibm.ws.ejbcontainer.fat_tools are available.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/test/com.ibm.ws.ejbcontainer.fat_tools/maven-metadata.xml
       - http://public.dhe.ibm.com/ibmdl/export/pub/software/olrepo/test/com.ibm.ws.ejbcontainer.fat_tools/maven-metadata.xml
       - file:/Users/aguibert/dev/git/open-liberty/dev/cnf/local/test/com.ibm.ws.ejbcontainer.fat_tools/
       - file:/Users/aguibert/dev/git/open-liberty/dev/cnf/local/test/com.ibm.ws.ejbcontainer.fat_tools/1.0.33.201909241016/ivy-1.0.33.201909241016.xml
     Required by:
         project :com.ibm.ws.ejbcontainer.async_fat
Run Code Online (Sandbox Code Playgroud)

语境

目前我的项目正在使用 Gradle 5.5,并且可以使用 Java 8、11 或 12 构建。我也试图让它与 Java 13 一起工作,所以我正在尝试升级到 Gradle 6.0。

现在似乎通​​配符依赖项在 Gradle 中的工作方式发生了一般的行为变化(例如com.foo:bar:1.+)。

And*_*ert 7

根据这个 Gradle 问题,Gradle 6.0 中的行为发生了重大变化。以前,Gradle 会自动检查工件元数据(例如maven-metadata.xml),但为了提高性能,Gradle 6.0 似乎不再默认这样做。

这个问题有两种可能的解决方案:

  1. 使用特定的依赖坐标而不是通配符版本1.+(这是 IMO 的最佳实践)

  2. 更新repositories.[maven|ivy].metadataSources配置。在 Gradle 5.X 中,默认值是:

    repositories {
        maven {
            url "http://repo.mycompany.com/repo"
            metadataSources {
                mavenPom()
                artifact()
            }
        }
        ivy {
            url "http://repo.mycompany.com/repo"
            metadataSources {
                ivyDescriptor()
                artifact()
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    但是在 Gradle 6.0 中,它们现在是:

    repositories {
        maven {
            url "http://repo.mycompany.com/repo"
            metadataSources {
                mavenPom()
            }
        }
        ivy {
            url "http://repo.mycompany.com/repo"
            metadataSources {
                ivyDescriptor()
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    因此,要恢复到以前的行为,请将artifact()配置添加到repositores.[maven|ivy].metadataSources配置块。