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 31, 2025
1 parent 72865f3 commit 9e3b1e0
Show file tree
Hide file tree
Showing 12 changed files with 604 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

import org.apache.maven.api.annotations.Nonnull;

/**
* Represents a repository backed by an IDE workspace, the output of a build session,
* or similar ad-hoc collections of artifacts. This repository is considered read-only
* within the context of a session, meaning it can only be used for artifact resolution,
* not for installation or deployment. This interface does not provide direct access
* to artifacts; that functionality is handled by a {@code WorkspaceReader}.
*/
public interface WorkspaceRepository extends Repository {

/**
* {@return the type of the repository, i.e. "workspace"}
*/
@Nonnull
@Override
default String getType() {
return "workspace";
}
}
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 @@ -29,8 +29,26 @@
import org.apache.maven.api.annotations.Nonnull;

/**
* Manages the organization and access of artifacts within the local Maven repository.
* The local repository serves as a cache for downloaded remote artifacts and storage
* for locally installed artifacts. This manager provides services to determine the
* appropriate paths for artifacts within the local repository structure.
*
* <p>The LocalRepositoryManager is responsible for:
* <ul>
* <li>Determining the storage path for locally installed artifacts</li>
* <li>Managing the layout and organization of cached remote artifacts</li>
* <li>Maintaining consistency in artifact storage patterns</li>
* </ul>
*
* <p>This interface is part of Maven's repository management system and works in
* conjunction with {@link RemoteRepository} and {@link LocalRepository} to provide
* a complete artifact resolution and storage solution.
*
* @since 4.0.0
* @see LocalRepository
* @see RemoteRepository
* @see Artifact
*/
@Experimental
public interface LocalRepositoryManager extends Service {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
import javax.inject.Inject;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.SimpleLookup;
import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.services.ArtifactManager;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.SwitchableMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
Expand All @@ -34,7 +40,12 @@
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.impl.DefaultArtifact;
import org.apache.maven.impl.DefaultArtifactCoordinatesFactory;
import org.apache.maven.impl.DefaultArtifactResolver;
import org.apache.maven.impl.DefaultModelVersionParser;
import org.apache.maven.impl.DefaultRepositoryFactory;
import org.apache.maven.impl.DefaultVersionParser;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.internal.impl.DefaultSession;
import org.apache.maven.model.Dependency;
Expand All @@ -52,6 +63,7 @@
import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -76,6 +88,9 @@ class LegacyRepositorySystemTest {
@Inject
private PlexusContainer container;

@Inject
private org.eclipse.aether.RepositorySystem resolverRepositorySystem;

protected List<ArtifactRepository> getRemoteRepositories() throws Exception {
File repoDir = new File(getBasedir(), "src/test/remote-repo").getAbsoluteFile();

Expand Down Expand Up @@ -129,11 +144,32 @@ void testThatASystemScopedDependencyIsNotResolvedFromRepositories() throws Excep
legacySupport.setSession(mavenSession);
InternalSession iSession = new DefaultSession(
mavenSession,
resolverRepositorySystem,
null,
null,
null,
new SimpleLookup(List.of(new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider())))),
new SimpleLookup(List.of(
new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider())),
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme())),
new DefaultArtifactCoordinatesFactory(),
new DefaultArtifactResolver(),
new ArtifactManager() {
private final Map<String, Path> paths = new ConcurrentHashMap<>();

@Override
public Optional<Path> getPath(org.apache.maven.api.Artifact artifact) {
Path path = paths.get(artifact.key());
if (path == null && artifact instanceof DefaultArtifact defaultArtifact) {
path = defaultArtifact.getArtifact().getPath();
}
return Optional.ofNullable(path);
}

@Override
public void setPath(ProducedArtifact artifact, Path path) {
paths.put(artifact.key(), path);
}
})),
null);
InternalSession.associate(session, iSession);

Expand Down
Loading

0 comments on commit 9e3b1e0

Please sign in to comment.