Prefer remote dependency over linked project

I want to configure dependency resolution so that it prefers remote version over linked project

From Gradle picks wrong jars in multi-module project with diamond dependencies - #2 by Schalk_Cronje

If the dependency is found in a linked project it will be preferred over the remote version (unless you have configured it not to).

It doesn’t link to documentation backing the statement, nor it explains how to configure dependency resolution to prefer remote over linked project.

My setup looks as follows

group=my.group
version=0.0.1

ProjectB
    b-module

ProjectA
   includeBuild ../ProjectB

   dependencies {
        api("my.group:b-module:version")

        api("my.group:other-module:version")
    }

OtherProject (not included in ProjectA)
    other-module

When releasing, I first publish ProjectB & OtherProject to maven, followed by ProjectA. This works fine.

The thing is, now I want to publish snapshot versions. Because it’s a complex project with multiple includeBuild, I can’t append -SNAPSHOT to every internal dependency, and I’m trying to apply dependency substitution rules

configurations.configureEach {
    resolutionStrategy.dependencySubstitution { dependencySubstitutions ->
        all { dependency ->
            def requestedDependency = dependency.requested
            if (requestedDependency instanceof ModuleComponentSelector
                    && requestedDependency.group == "my.group"
            ) {
                def snapshotVersion = "${requestedDependency.version}-SNAPSHOT"
                def target = "${requestedDependency.group}:${requestedDependency.module}:$snapshotVersion"
                
                dependency.useTarget(target)
            }
        }
    }
}

The issue is

  1. when I resolve b-module dependency, it always returns the project
  2. The requested dependency doesn’t include the -SNAPSHOT suffix.
publishing {
    publications {
        withType(MavenPublication.class).configureEach {
            versionMapping {
                ...
                usage(Usage.JAVA_RUNTIME) {
                    fromResolutionOf("releaseRuntimeClasspath")

                    def resolved = it.maybeResolveVersion("my.group", "b-module", "0.0.1-SNAPSHOT")
                    println("Resolved: $resolved")

                   ResolutionResult resolutionResult = it.targetConfiguration
                            .getIncoming()
                            .getResolutionResult();

                    resolutionResult.getAllDependencies().forEach { dependencyResult ->
                        if (dependencyResult.getRequested().getGroup() == "my.group") {
                            println("Dependency is '${dependencyResult.getRequested()}'")
                        }
                   }
                }
        }
    }
}

// Resolved: project :project-b:b-module
// Dependency is 'my.group:b-module:0.0.1`

Any hint will be greatly appreciated :pray:

If I remove the includeBuild, my setup to publish snapshots works fine.

I didn’t read the full post, but the link you ask for in the beginning is most probably ResolutionStrategy#getUseGlobalDependencySubstitutionRules which you then would set to false. Maybe that helps you resolve your problem. :slight_smile:

2 Likes

That did the trick :heart_eyes:

I think it’s the 3rd time you save my day on this forum, so huge thanks to you :bowing_man:

1 Like