Skip to content

Augment version range resolution used repositories #2574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -55,6 +56,7 @@
import org.eclipse.aether.spi.locator.Service;
import org.eclipse.aether.spi.locator.ServiceLocator;
import org.eclipse.aether.spi.synccontext.SyncContextFactory;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
Expand All @@ -68,6 +70,16 @@
@Named
@Singleton
public class DefaultVersionRangeResolver implements VersionRangeResolver, Service {
/**
* Configuration property for version range resolution used metadata {@link Metadata.Nature}.
* It may contain string names of {@link Metadata.Nature} enum values, or string value {@code "auto"}
* to decide based on range: if any of the boundary versions is snapshot, {@link Metadata.Nature#RELEASE_OR_SNAPSHOT}
* will be used, otherwise {@link Metadata.Nature#RELEASE}.
* Default (when unset) is existing Maven 3 behaviour, using {@link Metadata.Nature#RELEASE_OR_SNAPSHOT}.
*
* @since 3.9.11
*/
public static final String MAVEN_VERSION_RANGE_RESOLUTION_NATURE = "maven.versionRangeResolutionNature";

private static final String MAVEN_METADATA_XML = "maven-metadata.xml";

Expand Down Expand Up @@ -138,11 +150,34 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V
result.addVersion(versionConstraint.getVersion());
} else {
VersionRange.Bound lowerBound = versionConstraint.getRange().getLowerBound();
if (lowerBound != null
&& lowerBound.equals(versionConstraint.getRange().getUpperBound())) {
VersionRange.Bound upperBound = versionConstraint.getRange().getUpperBound();
if (lowerBound != null && lowerBound.equals(upperBound)) {
result.addVersion(lowerBound.getVersion());
} else {
Map<String, ArtifactRepository> versionIndex = getVersions(session, result, request);
Metadata.Nature wantedNature;
String natureString = ConfigUtils.getString(
session, Metadata.Nature.RELEASE_OR_SNAPSHOT.name(), MAVEN_VERSION_RANGE_RESOLUTION_NATURE);
if ("auto".equals(natureString)) {
org.eclipse.aether.artifact.Artifact lowerArtifact = lowerBound != null
? request.getArtifact()
.setVersion(lowerBound.getVersion().toString())
: null;
org.eclipse.aether.artifact.Artifact upperArtifact = upperBound != null
? request.getArtifact()
.setVersion(upperBound.getVersion().toString())
: null;

if (lowerArtifact != null && lowerArtifact.isSnapshot()
|| upperArtifact != null && upperArtifact.isSnapshot()) {
wantedNature = Metadata.Nature.RELEASE_OR_SNAPSHOT;
} else {
wantedNature = Metadata.Nature.RELEASE;
}
} else {
wantedNature = Metadata.Nature.valueOf(natureString.toUpperCase(Locale.ROOT));
}

Map<String, ArtifactRepository> versionIndex = getVersions(session, result, request, wantedNature);

List<Version> versions = new ArrayList<>();
for (Map.Entry<String, ArtifactRepository> v : versionIndex.entrySet()) {
Expand All @@ -166,7 +201,10 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V
}

private Map<String, ArtifactRepository> getVersions(
RepositorySystemSession session, VersionRangeResult result, VersionRangeRequest request) {
RepositorySystemSession session,
VersionRangeResult result,
VersionRangeRequest request,
Metadata.Nature wantedNature) {
RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);

Map<String, ArtifactRepository> versionIndex = new HashMap<>();
Expand All @@ -175,7 +213,7 @@ private Map<String, ArtifactRepository> getVersions(
request.getArtifact().getGroupId(),
request.getArtifact().getArtifactId(),
MAVEN_METADATA_XML,
Metadata.Nature.RELEASE_OR_SNAPSHOT);
wantedNature);

List<MetadataRequest> metadataRequests =
new ArrayList<>(request.getRepositories().size());
Expand Down