|
| 1 | +package org.jenkinsci.maven.plugins.hpi; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import org.apache.maven.RepositoryUtils; |
| 6 | +import org.apache.maven.plugin.MojoExecutionException; |
| 7 | +import org.apache.maven.plugins.annotations.Component; |
| 8 | +import org.eclipse.aether.RepositorySystem; |
| 9 | +import org.eclipse.aether.RepositorySystemSession; |
| 10 | +import org.eclipse.aether.collection.CollectRequest; |
| 11 | +import org.eclipse.aether.graph.Dependency; |
| 12 | +import org.eclipse.aether.graph.DependencyNode; |
| 13 | +import org.eclipse.aether.resolution.DependencyRequest; |
| 14 | +import org.eclipse.aether.resolution.DependencyResolutionException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Base class for Mojos that need to traverse the dependency graph using the |
| 18 | + * Maven Resolver API. |
| 19 | + * This replaces the old AbstractDependencyGraphTraversingMojo that used |
| 20 | + * maven-dependency-tree. |
| 21 | + */ |
| 22 | +public abstract class ResolverDependencyGraphTraversingMojo extends AbstractJenkinsMojo { |
| 23 | + |
| 24 | + @Component |
| 25 | + protected RepositorySystem resolverRepositorySystem; |
| 26 | + |
| 27 | + /** |
| 28 | + * Traverses the whole dependency tree rooted at the project. |
| 29 | + */ |
| 30 | + protected void traverseProject() throws MojoExecutionException { |
| 31 | + try { |
| 32 | + RepositorySystemSession repositorySystemSession = session.getRepositorySession(); |
| 33 | + |
| 34 | + // Convert Maven project to Resolver dependencies |
| 35 | + List<Dependency> dependencies = project.getDependencies().stream() |
| 36 | + .map(dep -> RepositoryUtils.toDependency( |
| 37 | + dep, session.getRepositorySession().getArtifactTypeRegistry())) |
| 38 | + .collect(Collectors.toList()); |
| 39 | + |
| 40 | + // Create collect request |
| 41 | + CollectRequest collectRequest = new CollectRequest(); |
| 42 | + collectRequest.setRoot(new Dependency(RepositoryUtils.toArtifact(project.getArtifact()), null)); |
| 43 | + collectRequest.setDependencies(dependencies); |
| 44 | + if (project.getDependencyManagement() != null) { |
| 45 | + collectRequest.setManagedDependencies(project.getDependencyManagement().getDependencies().stream() |
| 46 | + .map(dep -> RepositoryUtils.toDependency( |
| 47 | + dep, session.getRepositorySession().getArtifactTypeRegistry())) |
| 48 | + .collect(Collectors.toList())); |
| 49 | + } |
| 50 | + collectRequest.setRepositories(RepositoryUtils.toRepos(project.getRemoteArtifactRepositories())); |
| 51 | + |
| 52 | + // Create dependency request |
| 53 | + DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null); |
| 54 | + |
| 55 | + // Resolve dependencies |
| 56 | + org.eclipse.aether.resolution.DependencyResult result = |
| 57 | + resolverRepositorySystem.resolveDependencies(repositorySystemSession, dependencyRequest); |
| 58 | + |
| 59 | + // Traverse the resolved dependency tree |
| 60 | + visit(result.getRoot()); |
| 61 | + |
| 62 | + } catch (DependencyResolutionException e) { |
| 63 | + throw new MojoExecutionException("Failed to resolve dependencies", e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Traverses a tree rooted at the given node. |
| 69 | + */ |
| 70 | + protected void visit(DependencyNode node) { |
| 71 | + visit(node, null); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Traverses a tree rooted at the given node with parent context. |
| 76 | + */ |
| 77 | + protected void visit(DependencyNode node, DependencyNode parent) { |
| 78 | + if (accept(node, parent)) { |
| 79 | + for (DependencyNode child : node.getChildren()) { |
| 80 | + visit(child, node); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Visits a node. Called at most once for any node in the dependency tree. |
| 87 | + * |
| 88 | + * @param node the current node |
| 89 | + * @param parent the parent node (null for root) |
| 90 | + * @return true if the children should be traversed. |
| 91 | + */ |
| 92 | + protected abstract boolean accept(DependencyNode node, DependencyNode parent); |
| 93 | +} |
0 commit comments