Gradle unable to find transitive dependency when transitive dependency has version as a variable

117 views Asked by At

build.gradle has the following dependencies -

dependencies {
    compile("com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT")
}

In my maven repository, I have the following files at location REPO_URL/com/rhuldip/artifactId/com.rhuldip.mainDependency/1.0.0-SNAPSHOT

  1. maven-metadata.XML
  2. com.rhuldip.mainDependency-1.0.0.pom
  3. com.rhuldip.mainDependency-1.0.0.jar

The file com.rhuldip.mainDependency-1.0.0.pom contains transitive dependency and a variable like this -

<properties>
    <myapp.version>${revision}</myapp.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.rhuldip.myapp</groupId>
            <artifactId>transitiveDependeny</artifactId>
            <version>${myapp.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

But I am getting this error -

***
01:08:40    > Could not resolve com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT.
01:08:40       > Could not parse POM REPO-URL/com/rhuldip/artifactId/com.rhuldip.mainDependency/1.0.0-SNAPSHOT/com.rhuldip.mainDependency-1.0.0-SNAPSHOT.pom
01:08:40          > Could not find com.rhuldip.myapp:transitiveDependeny:${revision}.

I am using gradle 4.10.2 version. I tried sending a revision variable like this but still getting the same error

ext {
    revision: '1.1.0-SNAPSHOT'
}
1

There are 1 answers

5
VonC On

I suppose you have:

Gradle Project
│
└── build.gradle
    └── dependencies
        └── com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT
            └── Maven Repository
                ├── com.rhuldip.mainDependency-1.0.0.pom
                │   └── transitiveDependeny with ${revision} as version
                └── com.rhuldip.mainDependency-1.0.0.jar

You need to explicitly define the revision property in your Gradle build script so that it can be properly resolved when fetching the transitive dependency.
But: Gradle 4.10.2 (2018!) does not automatically pass properties defined in the ext block to the Maven POM resolver. You can try instead defining this property at the top level of your build script.

A resolution strategy to substitute the variable in the transitive dependency version could help... even in 4.10.2.
Use the ResolutionStrategy in your build.gradle file to dynamically replace the version of the transitive dependency.
Implement the strategy to replace the dynamic version (${revision}) with a specific version for transitiveDependeny.

Your build.gradle file would be:

ext {
    myappVersion = '1.1.0-SNAPSHOT' // Define the specific version you want to use
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'com.rhuldip.myapp' && details.requested.name == 'transitiveDependeny') {
            if (details.requested.version == '${revision}') {
                details.useVersion myappVersion
            }
        }
    }
}

dependencies {
    compile("com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT")
}

That configuration in your build.gradle tells Gradle to replace the ${revision} placeholder in the version of transitiveDependeny with the specific version 1.1.0-SNAPSHOT.


As an alternative workaround, you can explicitly declare the transitive dependency in your build.gradle file with the correct version. That way, Gradle will not rely on the POM file to resolve the version. And modify your build.gradle to include the transitive dependency with a specific version.

dependencies {
    compile("com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT")
    // Explicitly specify the version for the transitive dependency
    compile("com.rhuldip.myapp:transitiveDependeny:1.1.0-SNAPSHOT")
}

That approach essentially bypasses the issue by not relying on the Maven POM file to resolve the dynamic version of transitiveDependeny. Instead, you are directly specifying the dependency and its version in your Gradle project.


Still getting the same error.

Also, The dependency is in a remote maven repository and com.rhuldip.artifactId:com.rhuldip.mainDependency has a transitive dependency with artifactId com.rhuldip.myapp and name as transitiveDependency.
The dependency is declared in com.rhuldip.artifactId:com.rhuldip.mainDependency-1.0.0.pom, but the actual location of the transitive dependency is at REO_URL/com/rhuldip/myapp.

So the transitive dependency is hosted in a remote Maven repository. That means the repository configuration in your Gradle project must correctly point to this remote repository.

And the problem is that the version of the transitive dependency is specified as a variable (${revision}) in the POM file of com.rhuldip.artifactId:com.rhuldip.mainDependency.

Make sure your build.gradle includes the remote Maven repository where com.rhuldip.myapp is located:

repositories {
    maven {
        url 'REPO_URL' // Replace with the actual URL of your remote Maven repository
    }
    // Include other necessary repositories, like mavenCentral, if needed
}

Since the resolution strategy to replace the ${revision} placeholder does not work as expected, try and explicitly declare the transitive dependency with a fixed version, as previously mentioned. That is a more direct approach and can sometimes bypass issues with variable resolution in POM files.

Define the revision variable in a gradle.properties file, which might be more effective in some setups.

Your build.gradle would be:

dependencies {
    compile("com.rhuldip.artifactId:com.rhuldip.mainDependency:1.0.0-SNAPSHOT")
    // Explicitly define the transitive dependency
    compile("com.rhuldip.myapp:transitiveDependency:1.1.0-SNAPSHOT")
}

And the gradle.properties file:

revision=1.1.0-SNAPSHOT

But make sure the version 1.1.0-SNAPSHOT of com.rhuldip.myapp:transitiveDependency is indeed available in the remote repository. If it is not, replace this with the correct available version.