Search code examples
gradletransitive-dependencymaven

Gradle cannot resolve transitive dependency, but direct dependency works


I have a Gradle build script containing the following dependencies (and not much else):

dependencies {
    testCompile "com.foo:lib-foo:2.0.0-SNAPSHOT"
    testCompile "com.foo:lib-bar:2.0.2-SNAPSHOT"
}

The dependencies are resolved from a Maven repository (Sonatyp Nexus OSS). lib-bar has a dependency on lib-foo declared in its pom.xml:

<dependency>
    <groupId>com.foo</groupId>
    <artifactId>lib-foo</artifactId>
    <version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>

When I remove the dependency on lib-bar from my build script (and all code that uses it), everything compiles fine. When the I declare dependency, Gradle complains:

Could not resolve all dependencies for configuration ':testCompile'.
> Could not find any version that matches com.foo:lib-foo:[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT).
  Required by:
      :my-project:unspecified > com.foo:lib-bar:2.0.2-SNAPSHOT

The same scenario works fine with Maven.

The tutorial does not mention any limitations on Gradles ability to handle transitive dependencies or version ranges that would explain this, so I assumed this would work. Am I using it wrong? How can I make Gradle resolve that dependency?

P.S.:

When I run gradle test --info I get this:

Resource missing. [HTTP HEAD: https://nexus.foo.com/nexus/content/groups/public/com/foo/lib-foo/2.0.0-SNAPSHOT/lib-foo-2.0.0-SNAPSHOT.pom]
Resource missing. [HTTP HEAD: https://nexus.foo.com/nexus/content/groups/public/com/foo/lib-foo/2.0.0-SNAPSHOT/lib-foo-2.0.0-SNAPSHOT.jar]

These resources do indeed not exists, because the snapshots have file names with timestamps. But shouldn't Gradle use maven-metadata.xml to resolve that? And why does it work when I declare the dependency directly?


Solution

  • While Peter provided useful insight and the final hint (RTFM), he did not post the solution, so here it comes:

    dependencies {
        testCompile "com.foo:lib-foo:2.0.0-SNAPSHOT"
        testCompile("com.foo:lib-bar:2.0.2-SNAPSHOT") {
            transitive = false
        }
    }
    

    This tells Gradle to not resolve transitive dependencies for lib-bar. This works without modifications in this case because the only transitive dependency of lib-bar is already included.