Skip to content

Commit

Permalink
Add missing infos to ArtifactResolverResult, use ArtifactResolver ins…
Browse files Browse the repository at this point in the history
…ide DefaultProjectBuilder
  • Loading branch information
gnodet committed Jan 30, 2025
1 parent 72865f3 commit aa5cfd7
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api;

public interface WorkspaceRepository extends Repository {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ public class ArtifactResolverException extends MavenException {
@Serial
private static final long serialVersionUID = 7252294837746943917L;

private final ArtifactResolverResult result;

/**
* @param message the message for the exception
* @param e the exception itself
* @param result the resolution result containing detailed information
*/
public ArtifactResolverException(String message, Exception e) {
public ArtifactResolverException(String message, Exception e, ArtifactResolverResult result) {
super(message, e);
this.result = result;
}

public ArtifactResolverResult getResult() {
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface ArtifactResolverRequest extends Request<Session> {
@Nonnull
Collection<? extends ArtifactCoordinates> getCoordinates();

@Nonnull
@Nullable
List<RemoteRepository> getRepositories();

@Nonnull
Expand Down Expand Up @@ -155,6 +155,7 @@ public int hashCode() {
}

@Override
@Nonnull
public String toString() {
return "ArtifactResolverRequest[" + "coordinates="
+ coordinates + ", repositories="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,118 @@

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.maven.api.Artifact;
import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.DownloadedArtifact;
import org.apache.maven.api.Repository;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* The Artifact Result
* Represents the result of resolving an artifact, acting as a wrapper over {@link ArtifactResult}.
* <p>
* This interface provides access to resolved artifacts, their associated paths, and any related exceptions that
* occurred during the resolution process.
* </p>
*
* @since 4.0.0
*/
@Experimental
public interface ArtifactResolverResult extends Result<ArtifactResolverRequest> {

/**
* @return {@link Artifact}
* Returns a collection of resolved artifacts.
*
* @return A collection of {@link DownloadedArtifact} instances representing the resolved artifacts.
*/
@Nonnull
Collection<DownloadedArtifact> getArtifacts();

/**
* Retrieves the file system path associated with a specific artifact.
*
* @param artifact The {@link Artifact} whose path is to be retrieved.
* @return The {@link Path} to the artifact, or {@code null} if unavailable.
*/
@Nullable
Path getPath(Artifact artifact);
Path getPath(@Nonnull Artifact artifact);

/**
* Returns a mapping of artifact coordinates to their corresponding resolution results.
*
* @return A {@link Map} where keys are {@link ArtifactCoordinates} and values are {@link ResultItem} instances.
*/
@Nonnull
Map<? extends ArtifactCoordinates, ResultItem> getResults();

/**
* Retrieves the resolution result for a specific set of artifact coordinates.
*
* @param coordinates The {@link ArtifactCoordinates} identifying the artifact.
* @return The corresponding {@link ResultItem}, or {@code null} if no result exists.
*/
default ResultItem getResult(ArtifactCoordinates coordinates) {
return getResults().get(coordinates);
}

/**
* Represents an individual resolution result for an artifact.
*/
interface ResultItem {

/**
* Returns the coordinates of the resolved artifact.
*
* @return The {@link ArtifactCoordinates} of the artifact.
*/
ArtifactCoordinates getCoordinates();

/**
* Returns the resolved artifact.
*
* @return The {@link DownloadedArtifact} instance.
*/
DownloadedArtifact getArtifact();

/**
* Returns a mapping of repositories to the exceptions encountered while resolving the artifact.
*
* @return A {@link Map} where keys are {@link Repository} instances and values are {@link Exception} instances.
*/
Map<Repository, List<Exception>> getExceptions();

/**
* Returns the repository from which the artifact was resolved.
*
* @return The {@link Repository} instance.
*/
Repository getRepository();

/**
* Returns the file system path to the resolved artifact.
*
* @return The {@link Path} to the artifact.
*/
Path getPath();

/**
* Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully
* resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some
* of the specified remote repositories.
*
* @return {@code true} if the artifact was resolved, {@code false} otherwise.
*/
boolean isResolved();

/**
* Indicates whether the requested artifact is not present in any of the specified repositories.
*
* @return {@code true} if the artifact is not present in any repository, {@code false} otherwise.
*/
boolean isMissing();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import org.apache.maven.ProjectCycleException;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.Language;
import org.apache.maven.api.ProjectScope;
import org.apache.maven.api.SessionData;
Expand All @@ -60,6 +61,9 @@
import org.apache.maven.api.model.Plugin;
import org.apache.maven.api.model.Profile;
import org.apache.maven.api.model.ReportPlugin;
import org.apache.maven.api.services.ArtifactResolver;
import org.apache.maven.api.services.ArtifactResolverException;
import org.apache.maven.api.services.ArtifactResolverResult;
import org.apache.maven.api.services.BuilderProblem.Severity;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderException;
Expand All @@ -81,6 +85,7 @@
import org.apache.maven.impl.DefaultSourceRoot;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.resolver.ArtifactDescriptorUtils;
import org.apache.maven.internal.impl.InternalMavenSession;
import org.apache.maven.model.building.DefaultModelProblem;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelBuildingRequest;
Expand All @@ -92,9 +97,6 @@
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.eclipse.aether.repository.WorkspaceRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -408,15 +410,25 @@ ProjectBuildingResult build(Artifact artifact, boolean allowStubModel) throws Pr
boolean localProject;

try {
ArtifactRequest pomRequest = new ArtifactRequest();
pomRequest.setArtifact(pomArtifact);
pomRequest.setRepositories(RepositoryUtils.toRepos(request.getRemoteRepositories()));
ArtifactResult pomResult = repoSystem.resolveArtifact(session, pomRequest);

pomArtifact = pomResult.getArtifact();
localProject = pomResult.getRepository() instanceof WorkspaceRepository;
} catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
if (e.getResults().get(0).isMissing() && allowStubModel) {
InternalSession iSession = InternalSession.from(session);
ArtifactCoordinates coordinates = iSession.createArtifactCoordinates(iSession.getArtifact(pomArtifact));
org.apache.maven.api.services.ArtifactResolverRequest req =
org.apache.maven.api.services.ArtifactResolverRequest.builder()
.session(iSession)
.repositories(request.getRemoteRepositories().stream()
.map(RepositoryUtils::toRepo)
.map(iSession::getRemoteRepository)
.toList())
.coordinates(List.of(coordinates))
.build();
ArtifactResolverResult res =
iSession.getService(ArtifactResolver.class).resolve(req);
ArtifactResolverResult.ResultItem resItem = res.getResult(coordinates);

pomArtifact = InternalMavenSession.from(session).toArtifact(resItem.getArtifact());
localProject = resItem.getRepository() instanceof org.apache.maven.api.WorkspaceRepository;
} catch (ArtifactResolverException e) {
if (e.getResult().getResults().values().iterator().next().isMissing() && allowStubModel) {
return build(null, createStubModelSource(artifact));
}
throw new ProjectBuildingException(
Expand Down
Loading

0 comments on commit aa5cfd7

Please sign in to comment.