Skip to content

Commit de200b2

Browse files
authored
Fix MavenDependency#findNewerVersion ExactVersion edge cases (#7305)
Closes #5062 - Add early exit for ExactVersion before downloading metadata when the target version is not an upgrade from the current version, avoiding unnecessary HTTP traffic. - Fix the corrupt-metadata fallback to verify the exact version is actually an upgrade before returning it, instead of returning it unconditionally.
1 parent 365840d commit de200b2

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenDependency.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public class MavenDependency implements Trait<Xml.Tag> {
6868
return null;
6969
}
7070

71+
// For exact version, check upfront whether the target version is actually an
72+
// upgrade from the current version before downloading metadata
73+
if (versionComparator instanceof ExactVersion) {
74+
String exactVersion = ((ExactVersion) versionComparator).getVersion();
75+
if (exactVersion.equals(finalVersion) ||
76+
versionComparator.compare(finalVersion, finalVersion, exactVersion) > 0) {
77+
return null;
78+
}
79+
}
80+
7181
try {
7282
MavenExecutionContextView mctx = MavenExecutionContextView.view(ctx);
7383
MavenSettings settings = mctx.effectiveSettings(mrr);
@@ -105,7 +115,9 @@ public class MavenDependency implements Trait<Xml.Tag> {
105115
Pom pom = new MavenPomDownloader(emptyMap(), ctx,
106116
mrr.getMavenSettings(), mrr.getActiveProfiles()).download(new GroupArtifactVersion(groupId, artifactId, ((ExactVersion) versionComparator).getVersion()),
107117
null, null, mrr.getPom().getRepositories());
108-
if (pom.getGav().getVersion().equals(exactVersion)) {
118+
if (pom.getGav().getVersion().equals(exactVersion) &&
119+
!exactVersion.equals(finalVersion) &&
120+
versionComparator.compare(finalVersion, finalVersion, exactVersion) <= 0) {
109121
return exactVersion;
110122
}
111123
} catch (MavenDownloadingException e) {

0 commit comments

Comments
 (0)