Skip to content

Commit e722715

Browse files
committed
Consider changes to a dependency's target when managing its version
Previously, dependency management was determined used the requested selector. This meant that any changes made to the target selector were ignored. This would cause a problem if an earlier resolution strategy had changed the group ID or artifact ID of the dependency as the managed version would be determined using the original coordinates not the updated coordinates. This commit applies managed versions using the target selector, thereby ensuring that any earlier changes to the target are taken into account when determining the managed version. Fixes gh-383
1 parent 19824b8 commit e722715

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

src/main/java/io/spring/gradle/dependencymanagement/internal/VersionConfiguringAction.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.gradle.api.artifacts.Configuration;
2525
import org.gradle.api.artifacts.Dependency;
2626
import org.gradle.api.artifacts.DependencyResolveDetails;
27+
import org.gradle.api.artifacts.ModuleVersionSelector;
2728
import org.gradle.api.artifacts.ResolutionStrategy;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
@@ -59,44 +60,44 @@ class VersionConfiguringAction implements Action<DependencyResolveDetails> {
5960

6061
@Override
6162
public void execute(DependencyResolveDetails details) {
62-
logger.debug("Processing dependency '{}'", details.getRequested());
63-
if (isDependencyOnLocalProject(this.project, details)) {
64-
logger.debug("'{}' is a local project dependency. Dependency management has not been applied",
65-
details.getRequested());
63+
ModuleVersionSelector target = details.getTarget();
64+
logger.debug("Processing requested dependency '{}' with target '{}", details.getRequested(), target);
65+
if (isDependencyOnLocalProject(this.project, target)) {
66+
logger.debug("'{}' is a local project dependency. Dependency management has not been applied", target);
6667
return;
6768
}
68-
if (isDirectDependency(details) && Versions.isDynamic(details.getRequested().getVersion())) {
69+
if (isDirectDependency(target) && Versions.isDynamic(target.getVersion())) {
6970
logger.debug("'{}' is a direct dependency and has a dynamic version. "
70-
+ "Dependency management has not been applied", details.getRequested());
71+
+ "Dependency management has not been applied", target);
7172
return;
7273
}
73-
String version = this.dependencyManagementContainer.getManagedVersion(this.configuration,
74-
details.getRequested().getGroup(), details.getRequested().getName());
74+
String version = this.dependencyManagementContainer.getManagedVersion(this.configuration, target.getGroup(),
75+
target.getName());
7576
if (version != null) {
76-
logger.debug("Using version '{}' for dependency '{}'", version, details.getRequested());
77+
logger.debug("Using version '{}' for dependency '{}'", version, target);
7778
details.useVersion(version);
7879
return;
7980
}
80-
logger.debug("No dependency management for dependency '{}'", details.getRequested());
81+
logger.debug("No dependency management for dependency '{}'", target);
8182
}
8283

83-
private boolean isDirectDependency(DependencyResolveDetails details) {
84+
private boolean isDirectDependency(ModuleVersionSelector selector) {
8485
if (this.directDependencies == null) {
8586
Set<String> directDependencies = new HashSet<>();
8687
for (Dependency dependency : this.configuration.getAllDependencies()) {
8788
directDependencies.add(dependency.getGroup() + ":" + dependency.getName());
8889
}
8990
this.directDependencies = directDependencies;
9091
}
91-
return this.directDependencies.contains(getId(details));
92+
return this.directDependencies.contains(getId(selector));
9293
}
9394

94-
private boolean isDependencyOnLocalProject(Project project, DependencyResolveDetails details) {
95-
return this.localProjects.getNames().contains(getId(details));
95+
private boolean isDependencyOnLocalProject(Project project, ModuleVersionSelector selector) {
96+
return this.localProjects.getNames().contains(getId(selector));
9697
}
9798

98-
private String getId(DependencyResolveDetails details) {
99-
return details.getRequested().getGroup() + ":" + details.getRequested().getName();
99+
private String getId(ModuleVersionSelector selector) {
100+
return selector.getGroup() + ":" + selector.getName();
100101
}
101102

102103
ResolutionStrategy applyTo(Configuration c) {

src/test/java/io/spring/gradle/dependencymanagement/DependencyManagementPluginIntegrationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -524,6 +524,12 @@ void platformConstrainingATransitiveDependencyDoesNotAccidentallyExcludeThatDepe
524524
"kotlin-stdlib-common-1.9.10.jar", "annotations-13.0.jar");
525525
}
526526

527+
@Test
528+
void whenDependencyIsSubstitutedNewCoordinatesAreUsedForDependencyManagement() {
529+
this.gradleBuild.runner().withArguments("resolve").build();
530+
assertThat(readLines("resolved.txt")).containsExactly("bcprov-jdk18on-1.78.1.jar");
531+
}
532+
527533
private void writeLines(Path path, String... lines) {
528534
try {
529535
Path resolvedPath = this.gradleBuild.runner().getProjectDir().toPath().resolve(path);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
buildscript {
2+
project.configurations.all {
3+
resolutionStrategy.eachDependency {
4+
if (requested.group == "org.bouncycastle" && requested.name == "bcprov-jdk15on") {
5+
useTarget("org.bouncycastle:bcprov-jdk18on:1.78.1")
6+
}
7+
}
8+
}
9+
}
10+
11+
plugins {
12+
id "java"
13+
id "io.spring.dependency-management"
14+
}
15+
16+
/*
17+
configurations.all {
18+
resolutionStrategy.eachDependency {
19+
if (requested.group == "org.bouncycastle" && requested.name == "bcprov-jdk15on") {
20+
useTarget("org.bouncycastle:bcprov-jdk18on:1.78.1")
21+
}
22+
}
23+
}
24+
*/
25+
26+
repositories {
27+
mavenCentral()
28+
}
29+
30+
dependencies {
31+
implementation("org.bouncycastle:bcprov-jdk15on:1.70")
32+
}
33+
34+
task resolve {
35+
doFirst {
36+
def files = project.configurations.compileClasspath.resolve()
37+
def output = new File("${buildDir}/resolved.txt")
38+
output.parentFile.mkdirs()
39+
files.collect { it.name }.each { output << "${it}\n" }
40+
}
41+
}

0 commit comments

Comments
 (0)