Gradle 需要它的项目无法解析子项目依赖

Gradle subproject dependency is not resolvable by project that needs it

我有以下项目结构:

example
├── build.gradle
├── module1
│   ├── build.gradle
│   └── main
│       ├── java
│       │   ├── module-info.java
│       │   └── com.example.module1
│       │       └── Example.java
│       └── resources
│           └── application.yml
└── module2
    ├── build.gradle
    ├── main
    │   ├── java
    │   │   ├── module-info.java
    │   │   └── com.example.module2
    │   │       └── Example2.java
    └── test

模块 1 build.gradle

repositories {
    maven {
        url 'http://download.osgeo.org/webdav/geotools/'
        name 'Open Source Geospatial Foundation Repository'
    }

    maven {
        url 'https://repo.boundlessgeo.com/main/'
        name 'Boundless Maven Repository'
    }

    maven {
        url 'http://repo.boundlessgeo.com/snapshot'
        name 'Geotools SNAPSHOT repository'
        mavenContent {
            snapshotsOnly()
        }
    }

    mavenCentral()
    jcenter()
}

dependencies {
    implementation "org.geotools:gt-main:$geotoolsVersion"
}

模块 2 build.gradle(取决于模块 1)

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation project(':module1')
}

问题是在解析module2的依赖时,找不到module1的传递依赖,所以出现如下错误:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':module2'.
> Could not resolve all files for configuration ':module2:runtimeClasspath'.
   > Could not find org.geotools:gt-main:21.2.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/org/geotools/gt-main/21.2/gt-main-21.2.pom
       - https://repo.maven.apache.org/maven2/org/geotools/gt-main/21.2/gt-main-21.2.jar
       - https://jcenter.bintray.com/org/geotools/gt-main/21.2/gt-main-21.2.pom
       - https://jcenter.bintray.com/org/geotools/gt-main/21.2/gt-main-21.2.jar
     Required by:
         project :module2 > project :module1

看起来它只是使用 module2 中声明的存储库而不是 module1.

中声明的存储库来搜索 module1 的传递依赖

有趣的是,如果我将 module2 中的依赖项更改为:

dependencies {
    compileClasspath project(':module1')
}

依赖关系已解决。然而,这意味着在运行时,module1 不是类路径的一部分,因此 运行 应用程序仍然失败。

我该如何解决这个问题?

问题是项目依赖项在依赖时不会泄露其存储库位置。

修复方法是将存储库移至根目录 build.gradle。类似于:

subprojects {
  repositories {
    //https://docs.geotools.org/latest/userguide/build/maven/repositories.html
    maven {
      url 'http://download.osgeo.org/webdav/geotools/'
      name 'Open Source Geospatial Foundation Repository'
    }

    maven {
      url 'https://repo.boundlessgeo.com/main/'
      name 'Boundless Maven Repository'
    }
  }
}

查看以下 github 个问题:

https://github.com/gradle/gradle/issues/4106

https://github.com/gradle/gradle/issues/8811