|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Christoph Läubrich and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.m2e.pde.target.shared; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Queue; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.eclipse.aether.RepositoryException; |
| 24 | +import org.eclipse.aether.RepositorySystem; |
| 25 | +import org.eclipse.aether.RepositorySystemSession; |
| 26 | +import org.eclipse.aether.artifact.Artifact; |
| 27 | +import org.eclipse.aether.graph.DefaultDependencyNode; |
| 28 | +import org.eclipse.aether.graph.Dependency; |
| 29 | +import org.eclipse.aether.graph.DependencyNode; |
| 30 | +import org.eclipse.aether.repository.RemoteRepository; |
| 31 | +import org.eclipse.aether.resolution.ArtifactDescriptorRequest; |
| 32 | +import org.eclipse.aether.resolution.ArtifactDescriptorResult; |
| 33 | +import org.eclipse.aether.resolution.ArtifactRequest; |
| 34 | +import org.eclipse.aether.resolution.ArtifactResult; |
| 35 | + |
| 36 | +/** |
| 37 | + * Collector to collect (and filter) all transitive dependencies of a maven |
| 38 | + * target location |
| 39 | + */ |
| 40 | +public class MavenDependencyCollector { |
| 41 | + |
| 42 | + private static final Set<String> VALID_EXTENSIONS = Set.of("jar", "pom"); |
| 43 | + |
| 44 | + private RepositorySystem repoSystem; |
| 45 | + private RepositorySystemSession repositorySession; |
| 46 | + private List<RemoteRepository> repositories; |
| 47 | + private DependencyDepth depth; |
| 48 | + private Collection<String> dependencyScopes; |
| 49 | + |
| 50 | + public MavenDependencyCollector(RepositorySystem repoSystem, RepositorySystemSession repositorySession, |
| 51 | + List<RemoteRepository> repositories, DependencyDepth depth, Collection<String> dependencyScopes) { |
| 52 | + this.repoSystem = repoSystem; |
| 53 | + this.repositorySession = repositorySession; |
| 54 | + this.repositories = repositories; |
| 55 | + this.depth = depth; |
| 56 | + this.dependencyScopes = dependencyScopes; |
| 57 | + } |
| 58 | + |
| 59 | + public DependencyResult collect(Dependency root) throws RepositoryException { |
| 60 | + if (!isValidDependency(root)) { |
| 61 | + throw new RepositoryException( |
| 62 | + "Invalid root dependency: " + root + " allowed extensions are " + VALID_EXTENSIONS); |
| 63 | + } |
| 64 | + List<Artifact> artifacts = new ArrayList<>(); |
| 65 | + List<DependencyNode> nodes = new ArrayList<>(); |
| 66 | + ArtifactDescriptor rootDescriptor = readArtifactDescriptor(root, null, artifacts, nodes); |
| 67 | + if (depth == DependencyDepth.NONE) { |
| 68 | + return new DependencyResult(artifacts, rootDescriptor.node(), nodes); |
| 69 | + } |
| 70 | + if (depth == DependencyDepth.DIRECT) { |
| 71 | + for (Dependency dependency : rootDescriptor.dependencies()) { |
| 72 | + readArtifactDescriptor(dependency, rootDescriptor.node(), artifacts, nodes); |
| 73 | + } |
| 74 | + return new DependencyResult(artifacts, rootDescriptor.node(), nodes); |
| 75 | + } |
| 76 | + // Add all dependencies with BFS method |
| 77 | + Set<String> collected = new HashSet<>(); |
| 78 | + collected.add(getId(rootDescriptor.node().getDependency())); |
| 79 | + Queue<ArtifactDescriptor> queue = new LinkedList<>(); |
| 80 | + queue.add(rootDescriptor); |
| 81 | + while (!queue.isEmpty()) { |
| 82 | + ArtifactDescriptor current = queue.poll(); |
| 83 | + for (Dependency dependency : current.dependencies()) { |
| 84 | + if (isValidDependency(dependency) && collected.add(getId(dependency))) { |
| 85 | + ArtifactDescriptor dependencyDescriptor = readArtifactDescriptor(dependency, current.node(), |
| 86 | + artifacts, nodes); |
| 87 | + if (dependencyDescriptor != null) { |
| 88 | + queue.add(dependencyDescriptor); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return new DependencyResult(artifacts, rootDescriptor.node(), nodes); |
| 94 | + } |
| 95 | + |
| 96 | + private String getId(Dependency dependency) { |
| 97 | + Artifact artifact = dependency.getArtifact(); |
| 98 | + // This does not include the version so we always ever only collect one version |
| 99 | + // of an (transitive) artifact |
| 100 | + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" |
| 101 | + + artifact.getClassifier(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * This method reads the artifact descriptor and resolves the artifact. |
| 106 | + * |
| 107 | + * @param artifact the artifact to read its descriptor |
| 108 | + * @param artifacts |
| 109 | + * @param nodes |
| 110 | + * @return the resolved artifact and the list of (managed) dependencies |
| 111 | + * @throws RepositoryException |
| 112 | + */ |
| 113 | + private ArtifactDescriptor readArtifactDescriptor(Dependency dependency, DependencyNode parent, |
| 114 | + Collection<Artifact> artifacts, |
| 115 | + List<DependencyNode> nodes) throws RepositoryException { |
| 116 | + if (isValidScope(dependency) && isValidDependency(dependency)) { |
| 117 | + ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest(); |
| 118 | + descriptorRequest.setArtifact(dependency.getArtifact()); |
| 119 | + descriptorRequest.setRepositories(repositories); |
| 120 | + ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor(repositorySession, descriptorRequest); |
| 121 | + ArtifactRequest artifactRequest = new ArtifactRequest(); |
| 122 | + artifactRequest.setArtifact(result.getArtifact()); |
| 123 | + artifactRequest.setRepositories(repositories); |
| 124 | + ArtifactResult artifactResult = repoSystem.resolveArtifact(repositorySession, artifactRequest); |
| 125 | + Artifact resolved = artifactResult.getArtifact(); |
| 126 | + artifacts.add(resolved); |
| 127 | + DefaultDependencyNode dependencyNode = new DefaultDependencyNode( |
| 128 | + new Dependency(resolved, dependency.getScope())); |
| 129 | + nodes.add(dependencyNode); |
| 130 | + if (parent != null) { |
| 131 | + parent.getChildren().add(dependencyNode); |
| 132 | + } |
| 133 | + return new ArtifactDescriptor(dependencyNode, result.getDependencies(), result.getManagedDependencies()); |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + private boolean isValidDependency(Dependency dependency) { |
| 139 | + if (dependency.isOptional()) { |
| 140 | + // optional in maven means do not include in transitive dependency chains see |
| 141 | + // https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html |
| 142 | + return false; |
| 143 | + } |
| 144 | + return VALID_EXTENSIONS.contains(dependency.getArtifact().getExtension()); |
| 145 | + } |
| 146 | + |
| 147 | + private boolean isValidScope(Dependency dependency) { |
| 148 | + String scope = dependency.getScope(); |
| 149 | + if (scope == null || scope.isEmpty()) { |
| 150 | + return true; |
| 151 | + |
| 152 | + } |
| 153 | + return dependencyScopes.contains(scope); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments