Use v4 api to resolve project dependencies (#1409)

This commit is contained in:
Guillaume Nodet 2024-02-29 00:30:57 +01:00 committed by GitHub
parent cf60940497
commit 3c2f8ebdb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 39 additions and 44 deletions

View File

@ -510,7 +510,7 @@ public interface Session {
ProjectScope requireProjectScope(String id);
DependencyScope requireDependencyScope(String id);
DependencyScope requireDependencyScope(@Nonnull String id);
PathScope requirePathScope(String id);
}

View File

@ -560,7 +560,7 @@ public abstract class AbstractSession implements InternalSession {
@Override
public DependencyScope requireDependencyScope(String id) {
return DependencyScope.forId(id);
return DependencyScope.forId(nonNull(id, "id"));
}
@Override

View File

@ -21,21 +21,17 @@ package org.apache.maven.internal.impl;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Collection;
import java.util.List;
import org.apache.maven.api.Node;
import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.DependencyCollector;
import org.apache.maven.api.services.DependencyCollectorException;
import org.apache.maven.api.services.DependencyCollectorRequest;
import org.apache.maven.api.services.DependencyCollectorResult;
import org.apache.maven.api.services.*;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
@ -52,16 +48,31 @@ public class DefaultDependencyCollector implements DependencyCollector {
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
Artifact rootArtifact =
request.getRootArtifact().map(session::toArtifact).orElse(null);
Dependency root =
request.getRoot().map(d -> session.toDependency(d, false)).orElse(null);
Artifact rootArtifact;
DependencyCoordinate root;
Collection<DependencyCoordinate> dependencies;
Collection<DependencyCoordinate> managedDependencies;
List<RemoteRepository> remoteRepositories;
if (request.getProject().isPresent()) {
Project project = request.getProject().get();
rootArtifact = project.getPomArtifact();
root = null;
dependencies = project.getDependencies();
managedDependencies = project.getManagedDependencies();
remoteRepositories = session.getService(ProjectManager.class).getRemoteProjectRepositories(project);
} else {
rootArtifact = request.getRootArtifact().orElse(null);
root = request.getRoot().orElse(null);
dependencies = request.getDependencies();
managedDependencies = request.getManagedDependencies();
remoteRepositories = session.getRemoteRepositories();
}
CollectRequest collectRequest = new CollectRequest()
.setRootArtifact(rootArtifact)
.setRoot(root)
.setDependencies(session.toDependencies(request.getDependencies(), false))
.setManagedDependencies(session.toDependencies(request.getManagedDependencies(), true))
.setRepositories(session.toRepositories(session.getRemoteRepositories()));
.setRootArtifact(rootArtifact != null ? session.toArtifact(rootArtifact) : null)
.setRoot(root != null ? session.toDependency(root, false) : null)
.setDependencies(session.toDependencies(dependencies, false))
.setManagedDependencies(session.toDependencies(managedDependencies, true))
.setRepositories(session.toRepositories(remoteRepositories));
RepositorySystemSession systemSession = session.getSession();
if (request.getVerbose()) {

View File

@ -72,31 +72,8 @@ public class DefaultDependencyResolver implements DependencyResolver {
@Override
public DependencyResolverResult resolve(DependencyResolverRequest request)
throws DependencyCollectorException, DependencyResolverException, ArtifactResolverException {
nonNull(request, "request can not be null");
InternalSession session = InternalSession.from(request.getSession());
if (request.getProject().isPresent()) {
DependencyResolutionResult result = resolveDependencies(
request.getSession(), request.getProject().get(), request.getPathScope());
Map<org.eclipse.aether.graph.Dependency, org.eclipse.aether.graph.DependencyNode> nodes = stream(
result.getDependencyGraph())
.filter(n -> n.getDependency() != null)
.collect(Collectors.toMap(DependencyNode::getDependency, n -> n));
Node root = session.getNode(result.getDependencyGraph());
List<Node> dependencies = new ArrayList<>();
Map<Dependency, Path> artifacts = new LinkedHashMap<>();
List<Path> paths = new ArrayList<>();
for (org.eclipse.aether.graph.Dependency dep : result.getResolvedDependencies()) {
dependencies.add(session.getNode(nodes.get(dep)));
Path path = dep.getArtifact().getFile().toPath();
artifacts.put(session.getDependency(dep), path);
paths.add(path);
}
return new DefaultDependencyResolverResult(
result.getCollectionErrors(), root, dependencies, paths, artifacts);
}
nonNull(request, "request");
Session session = InternalSession.from(request.getSession());
DependencyCollectorResult collectorResult =
session.getService(DependencyCollector.class).collect(request);

View File

@ -182,7 +182,8 @@ public class DefaultProject implements Project {
@Nonnull
@Override
public DependencyScope getScope() {
return session.requireDependencyScope(dependency.getScope());
String scope = dependency.getScope() != null ? dependency.getScope() : "";
return session.requireDependencyScope(scope);
}
@Override

View File

@ -33,6 +33,7 @@ import java.util.zip.ZipEntry;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.services.ProjectManager;
import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.classrealm.ClassRealmManager;
@ -523,6 +524,11 @@ public class DefaultMavenPluginManager implements MavenPluginManager {
InternalSession sessionV4 = InternalSession.from(session.getSession());
Project project = sessionV4.getProject(session.getCurrentProject());
List<org.apache.maven.api.RemoteRepository> repos =
sessionV4.getService(ProjectManager.class).getRemoteProjectRepositories(project);
sessionV4 = InternalSession.from(sessionV4.withRemoteRepositories(repos));
org.apache.maven.api.MojoExecution execution = new DefaultMojoExecution(sessionV4, mojoExecution);
org.apache.maven.api.plugin.Log log = new DefaultLog(
LoggerFactory.getLogger(mojoExecution.getMojoDescriptor().getFullGoalName()));