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 3 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,15 @@
@Named
@Singleton
public class DefaultVersionRangeResolver implements VersionRangeResolver, Service {
/**
* User property for version range handling. It may contain values of {@link Metadata.Nature} enum, or
* value "auto" to decide based on range: if any of the boundary version is snapshot, snapshots will be
* allowed, otherwise not. Default (unset) is "behave as before", query all repositories available in scope,
* so even snapshot repositories.
*
* @since 3.9.11
*/
public static final String MAVEN_VERSION_RANGE_HANDLING = "maven.versionRangeResolutionNature";

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

Expand Down Expand Up @@ -138,11 +149,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 handling = ConfigUtils.getString(
session, Metadata.Nature.RELEASE_OR_SNAPSHOT.name(), MAVEN_VERSION_RANGE_HANDLING);
if ("auto".equals(handling)) {
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(handling.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 +200,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 +212,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