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

时间:2019-07-17 02:17:48

标签: gradle dependency-management geotools subproject

我有以下项目结构:

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

module1 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"
}

module2 build.gradle(取决于module1)

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

似乎它只是使用module1中声明的存储库而不是module2中的存储库来搜索module1的可传递部门。

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

dependencies {
    compileClasspath project(':module1')
}

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

我该如何解决?

1 个答案:

答案 0 :(得分:0)

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

解决方法是将存储库移至根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