Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,23 @@ Map<PathType, List<Path>> resolveDependencies(
List<Version> resolveVersionRange(@Nonnull ArtifactCoordinates artifact, List<RemoteRepository> repositories)
throws VersionResolverException;

/**
* Resolves the highest available version of a version range.
* The returned version is only dependent on the configured repositories and their contents.
* The supplied request may also refer to a single concrete version rather than a version range.
* In this case though, the result contains simply the (parsed) input version, regardless of the
* repositories and their contents.
*
* @param artifact the artifact for which to resolve the versions
* @param repositories the repositories to use, or the session repositories if {@code null}
* @return the highest resolved {@code Version}.
* @throws org.apache.maven.api.services.VersionRangeResolverException if the resolution failed
* @see org.apache.maven.api.services.VersionRangeResolver#resolve(Session, ArtifactCoordinates) (String)
*/
@Nonnull
Optional<Version> resolveHighestVersion(@Nonnull ArtifactCoordinates artifact, List<RemoteRepository> repositories)
throws VersionResolverException;

/**
* Parses the specified version string, for example "1.0".
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,67 @@
import org.apache.maven.api.annotations.Nonnull;

/**
* Represents the result of a version range resolution request. This interface provides access to
* information about resolved versions that match a version range constraint, including any exceptions
* that occurred during resolution, the available versions, and their source repositories.
*
* <p>The versions returned by this interface are guaranteed to be in ascending order.</p>
*
* @since 4.0.0
*/
@Experimental
public interface VersionRangeResolverResult {

/**
* Gets the exceptions that occurred while resolving the version range.
*
* @return The list of exceptions that occurred during resolution, never {@code null}
*/
@Nonnull
List<Exception> getExceptions();

/**
* Gets the versions (in ascending order) that matched the requested range.
*
* @return The list of matching versions, never {@code null}. An empty list indicates
* no versions matched the requested range.
*/
@Nonnull
List<Version> getVersions();

/**
* Gets the lowest version matching the requested range.
*
* @return An Optional containing the lowest matching version, or empty Optional if no versions
* matched the requested range
*/
@Nonnull
default Optional<Version> getLowerVersion() {
default Optional<Version> getLowestVersion() {
return getVersions().isEmpty()
? Optional.empty()
: Optional.of(getVersions().get(0));
}

/**
* Gets the highest version matching the requested range.
*
* @return An Optional containing the highest matching version, or empty Optional if no versions
* matched the requested range
*/
@Nonnull
default Optional<Version> getHigherVersion() {
default Optional<Version> getHighestVersion() {
return getVersions().isEmpty()
? Optional.empty()
: Optional.of(getVersions().get(getVersions().size() - 1));
}

/**
* Gets the repository from which the specified version was resolved.
*
* @param version The version whose source repository should be retrieved, must not be {@code null}
* @return An Optional containing the repository from which the version was resolved,
* or empty Optional if the repository is unknown
*/
@Nonnull
Optional<Repository> getRepository(Version version);
}
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,14 @@ public List<Version> resolveVersionRange(ArtifactCoordinates artifact, List<Remo
.getVersions();
}

@Override
public Optional<Version> resolveHighestVersion(ArtifactCoordinates artifact, List<RemoteRepository> repositories)
throws VersionResolverException {
return getService(VersionRangeResolver.class)
.resolve(this, artifact, repositories)
.getHighestVersion();
}

@Override
public Type requireType(String id) {
return getService(TypeRegistry.class).require(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.maven.api.DownloadedArtifact;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.Version;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.di.Named;
Expand Down Expand Up @@ -135,16 +134,14 @@ public ModelSource resolveModel(
artifactId,
version);
}
List<Version> versions = session.resolveVersionRange(coords, repositories);
if (versions.isEmpty()) {
throw new ModelResolverException(
"No versions matched the requested " + (type != null ? type + " " : "") + "version range '"
+ version + "'",
groupId,
artifactId,
version);
}
String newVersion = versions.get(versions.size() - 1).asString();
String newVersion = session.resolveHighestVersion(coords, repositories)
.orElseThrow(() -> new ModelResolverException(
"No versions matched the requested " + (type != null ? type + " " : "") + "version range '"
+ version + "'",
groupId,
artifactId,
version))
.asString();
if (!version.equals(newVersion)) {
resolvedVersion.accept(newVersion);
}
Expand Down