Skip to content

Commit

Permalink
Allow exact version upgrades to Maven versions that exist but are mis…
Browse files Browse the repository at this point in the history
…sing in maven metadata
  • Loading branch information
jkschneider committed Sep 12, 2024
1 parent 3e694bb commit 3be53f3
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package org.openrewrite.semver;

import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Validated;

/**
* Version selector for matching exact version: either explicitly prefixed with "=",
* or implicit default when no other version selectors match.
*/
@Getter
public class ExactVersion extends LatestRelease {
String version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.*;
import org.openrewrite.maven.utilities.RetainVersions;
import org.openrewrite.semver.ExactVersion;
import org.openrewrite.semver.LatestPatch;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
Expand All @@ -34,6 +36,7 @@
import java.util.*;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.internal.StringUtils.matchesGlob;

Expand Down Expand Up @@ -151,7 +154,7 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
// if the resolved dependency exists AND it does not represent an artifact that was parsed
// as a source file, attempt to find a new version.
try {
String newerVersion = findNewerVersion(d.getVersion(),
String newerVersion = findNewerVersion(d.getVersion(), getResolutionResult(),
() -> downloadMetadata(d.getGroupId(), d.getArtifactId(), ctx), versionComparator, ctx);
if (newerVersion != null) {
Optional<Xml.Tag> version = tag.getChild("version");
Expand Down Expand Up @@ -400,13 +403,16 @@ private String resolveVersion(String version) {
private @Nullable String findNewerVersion(String groupId, String artifactId, String version, ExecutionContext ctx)
throws MavenDownloadingException {
return UpgradeDependencyVersion.this.findNewerVersion(
version, () -> downloadMetadata(groupId, artifactId, ctx), versionComparator, ctx);
version, getResolutionResult(), () -> downloadMetadata(groupId, artifactId, ctx), versionComparator, ctx);
}
};
}

private @Nullable String findNewerVersion(
String version, MavenMetadataFailures.MavenMetadataDownloader download, VersionComparator versionComparator, ExecutionContext ctx) throws MavenDownloadingException {
String version,
MavenResolutionResult mrr,
MavenMetadataFailures.MavenMetadataDownloader download,
VersionComparator versionComparator, ExecutionContext ctx) throws MavenDownloadingException {
String finalVersion = !Semver.isVersion(version) ? "0.0.0" : version;

// in the case of "latest.patch", a new version can only be derived if the
Expand All @@ -423,6 +429,29 @@ private String resolveVersion(String version) {
versions.add(v);
}
}

// Some repositories will have corrupt or incomplete maven metadata which
// prevents an upgrade even to a fixed version. For example this metadata file is missing all versions after 2019:
// https://repository.mapr.com/nexus/content/groups/mapr-public/org/apache/hbase/hbase-annotations/maven-metadata.xml
if (versionComparator instanceof ExactVersion) {
String exactVersion = ((ExactVersion) versionComparator).getVersion();
if (!versions.contains(exactVersion)) {
try {
// This is a best effort attempt to see if the pom is there anyway, in spite of the
// fact that it's not in the metadata. Usually it won't be, only in situations like the
// MapR repository mentioned in the comment above will it be.
Pom pom = new MavenPomDownloader(emptyMap(), ctx,
mrr.getMavenSettings(), mrr.getActiveProfiles()).download(new GroupArtifactVersion(groupId, artifactId, ((ExactVersion) versionComparator).getVersion()),
null, null, mrr.getPom().getRepositories());
if (pom.getGav().getVersion().equals(exactVersion)) {
return exactVersion;
}
} catch (MavenDownloadingException e) {
return null;
}
}
}

// handle upgrades from non semver versions like "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
if (!Semver.isVersion(finalVersion) && !versions.isEmpty()) {
versions.sort(versionComparator);
Expand Down
Loading

0 comments on commit 3be53f3

Please sign in to comment.