首页 > 解决方案 > Gradle 子项目依赖项无法由需要它的项目解决

问题描述

我有以下项目结构:

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

模块1build.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"
}

模块build.gradle2(取决于模块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

看起来它只是在搜索使用 in而不是 inmodule1声明的存储库的传递 deps 。module2module1

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

dependencies {
    compileClasspath project(':module1')
}

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

我怎样才能解决这个问题?

标签: gradledependency-managementgeotoolssubproject

解决方案


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

解决方法是将存储库移动到 rootbuild.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


推荐阅读