use of org.sonatype.aether.graph.Dependency in project sonatype-aether by sonatype.
the class GetDependencyTree method main.
public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(GetDependencyTree.class.getSimpleName());
RepositorySystem system = Booter.newRepositorySystem();
RepositorySystemSession session = Booter.newRepositorySystemSession(system);
Artifact artifact = new DefaultArtifact("org.apache.maven:maven-aether-provider:3.0.2");
RemoteRepository repo = Booter.newCentralRepository();
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, ""));
collectRequest.addRepository(repo);
CollectResult collectResult = system.collectDependencies(session, collectRequest);
collectResult.getRoot().accept(new ConsoleDependencyGraphDumper());
}
use of org.sonatype.aether.graph.Dependency in project sonatype-aether by sonatype.
the class DefaultDependencyCollector method collectDependencies.
public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request) throws DependencyCollectionException {
session = optimizeSession(session);
RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
CollectResult result = new CollectResult(request);
DependencySelector depSelector = session.getDependencySelector();
DependencyManager depManager = session.getDependencyManager();
DependencyTraverser depTraverser = session.getDependencyTraverser();
Dependency root = request.getRoot();
List<RemoteRepository> repositories = request.getRepositories();
List<Dependency> dependencies = request.getDependencies();
List<Dependency> managedDependencies = request.getManagedDependencies();
GraphEdge edge = null;
if (root != null) {
VersionRangeResult rangeResult;
try {
VersionRangeRequest rangeRequest = new VersionRangeRequest(root.getArtifact(), request.getRepositories(), request.getRequestContext());
rangeRequest.setTrace(trace);
rangeResult = versionRangeResolver.resolveVersionRange(session, rangeRequest);
if (rangeResult.getVersions().isEmpty()) {
throw new VersionRangeResolutionException(rangeResult, "No versions available for " + root.getArtifact() + " within specified range");
}
} catch (VersionRangeResolutionException e) {
result.addException(e);
throw new DependencyCollectionException(result);
}
Version version = rangeResult.getVersions().get(rangeResult.getVersions().size() - 1);
root = root.setArtifact(root.getArtifact().setVersion(version.toString()));
ArtifactDescriptorResult descriptorResult;
try {
ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(root.getArtifact());
descriptorRequest.setRepositories(request.getRepositories());
descriptorRequest.setRequestContext(request.getRequestContext());
descriptorRequest.setTrace(trace);
if (isLackingDescriptor(root.getArtifact())) {
descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
} else {
descriptorResult = descriptorReader.readArtifactDescriptor(session, descriptorRequest);
}
} catch (ArtifactDescriptorException e) {
result.addException(e);
throw new DependencyCollectionException(result);
}
root = root.setArtifact(descriptorResult.getArtifact());
repositories = remoteRepositoryManager.aggregateRepositories(session, repositories, descriptorResult.getRepositories(), true);
dependencies = mergeDeps(dependencies, descriptorResult.getDependencies());
managedDependencies = mergeDeps(managedDependencies, descriptorResult.getManagedDependencies());
GraphNode node = new GraphNode();
node.setAliases(descriptorResult.getAliases());
node.setRepositories(request.getRepositories());
edge = new GraphEdge(node);
edge.setDependency(root);
edge.setRequestContext(request.getRequestContext());
edge.setRelocations(descriptorResult.getRelocations());
edge.setVersionConstraint(rangeResult.getVersionConstraint());
edge.setVersion(version);
} else {
edge = new GraphEdge(new GraphNode());
}
result.setRoot(edge);
boolean traverse = (root == null) || depTraverser.traverseDependency(root);
if (traverse && !dependencies.isEmpty()) {
DataPool pool = new DataPool(session);
EdgeStack edges = new EdgeStack();
edges.push(edge);
DefaultDependencyCollectionContext context = new DefaultDependencyCollectionContext(session, root, managedDependencies);
Args args = new Args(result, session, trace, pool, edges, context);
process(args, dependencies, repositories, depSelector.deriveChildSelector(context), depManager.deriveChildManager(context), depTraverser.deriveChildTraverser(context));
}
DependencyGraphTransformer transformer = session.getDependencyGraphTransformer();
try {
DefaultDependencyGraphTransformationContext context = new DefaultDependencyGraphTransformationContext(session);
result.setRoot(transformer.transformGraph(edge, context));
} catch (RepositoryException e) {
result.addException(e);
}
if (!result.getExceptions().isEmpty()) {
throw new DependencyCollectionException(result);
}
return result;
}
use of org.sonatype.aether.graph.Dependency in project sonatype-aether by sonatype.
the class DefaultDependencyCollector method process.
private void process(final Args args, List<Dependency> dependencies, List<RemoteRepository> repositories, DependencySelector depSelector, DependencyManager depManager, DependencyTraverser depTraverser) throws DependencyCollectionException {
nextDependency: for (Dependency dependency : dependencies) {
boolean disableVersionManagement = false;
List<Artifact> relocations = Collections.emptyList();
thisDependency: while (true) {
if (!depSelector.selectDependency(dependency)) {
continue nextDependency;
}
DependencyManagement depMngt = depManager.manageDependency(dependency);
String premanagedVersion = null;
String premanagedScope = null;
if (depMngt != null) {
if (depMngt.getVersion() != null && !disableVersionManagement) {
Artifact artifact = dependency.getArtifact();
premanagedVersion = artifact.getVersion();
dependency = dependency.setArtifact(artifact.setVersion(depMngt.getVersion()));
}
if (depMngt.getProperties() != null) {
Artifact artifact = dependency.getArtifact();
dependency = dependency.setArtifact(artifact.setProperties(depMngt.getProperties()));
}
if (depMngt.getScope() != null) {
premanagedScope = dependency.getScope();
dependency = dependency.setScope(depMngt.getScope());
}
if (depMngt.getExclusions() != null) {
dependency = dependency.setExclusions(depMngt.getExclusions());
}
}
disableVersionManagement = false;
boolean noDescriptor = isLackingDescriptor(dependency.getArtifact());
boolean traverse = !noDescriptor && depTraverser.traverseDependency(dependency);
VersionRangeResult rangeResult;
try {
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(dependency.getArtifact());
rangeRequest.setRepositories(repositories);
rangeRequest.setRequestContext(args.result.getRequest().getRequestContext());
rangeRequest.setTrace(args.trace);
Object key = args.pool.toKey(rangeRequest);
rangeResult = args.pool.getConstraint(key, rangeRequest);
if (rangeResult == null) {
rangeResult = versionRangeResolver.resolveVersionRange(args.session, rangeRequest);
args.pool.putConstraint(key, rangeResult);
}
if (rangeResult.getVersions().isEmpty()) {
throw new VersionRangeResolutionException(rangeResult, "No versions available for " + dependency.getArtifact() + " within specified range");
}
} catch (VersionRangeResolutionException e) {
addException(args.result, e);
continue nextDependency;
}
List<Version> versions = rangeResult.getVersions();
for (Version version : versions) {
Artifact originalArtifact = dependency.getArtifact().setVersion(version.toString());
Dependency d = dependency.setArtifact(originalArtifact);
ArtifactDescriptorResult descriptorResult;
{
ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(d.getArtifact());
descriptorRequest.setRepositories(repositories);
descriptorRequest.setRequestContext(args.result.getRequest().getRequestContext());
descriptorRequest.setTrace(args.trace);
if (noDescriptor) {
descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
} else {
Object key = args.pool.toKey(descriptorRequest);
descriptorResult = args.pool.getDescriptor(key, descriptorRequest);
if (descriptorResult == null) {
try {
descriptorResult = descriptorReader.readArtifactDescriptor(args.session, descriptorRequest);
args.pool.putDescriptor(key, descriptorResult);
} catch (ArtifactDescriptorException e) {
addException(args.result, e);
args.pool.putDescriptor(key, e);
continue;
}
} else if (descriptorResult == DataPool.NO_DESCRIPTOR) {
continue;
}
}
}
d = d.setArtifact(descriptorResult.getArtifact());
GraphNode node = args.edges.top().getTarget();
GraphEdge cycleEdge = args.edges.find(d.getArtifact());
if (cycleEdge != null) {
GraphEdge edge = new GraphEdge(cycleEdge.getTarget());
edge.setDependency(d);
edge.setScope(d.getScope());
edge.setPremanagedScope(premanagedScope);
edge.setPremanagedVersion(premanagedVersion);
edge.setRelocations(relocations);
edge.setVersionConstraint(rangeResult.getVersionConstraint());
edge.setVersion(version);
edge.setRequestContext(args.result.getRequest().getRequestContext());
node.getOutgoingEdges().add(edge);
continue;
}
if (!descriptorResult.getRelocations().isEmpty()) {
relocations = descriptorResult.getRelocations();
disableVersionManagement = originalArtifact.getGroupId().equals(d.getArtifact().getGroupId()) && originalArtifact.getArtifactId().equals(d.getArtifact().getArtifactId());
dependency = d;
continue thisDependency;
}
d = args.pool.intern(d.setArtifact(args.pool.intern(d.getArtifact())));
DependencySelector childSelector = null;
DependencyManager childManager = null;
DependencyTraverser childTraverser = null;
List<RemoteRepository> childRepos = null;
Object key = null;
boolean recurse = traverse && !descriptorResult.getDependencies().isEmpty();
if (recurse) {
DefaultDependencyCollectionContext context = args.collectionContext;
context.set(d, descriptorResult.getManagedDependencies());
childSelector = depSelector.deriveChildSelector(context);
childManager = depManager.deriveChildManager(context);
childTraverser = depTraverser.deriveChildTraverser(context);
childRepos = remoteRepositoryManager.aggregateRepositories(args.session, repositories, descriptorResult.getRepositories(), true);
key = args.pool.toKey(d.getArtifact(), childRepos, childSelector, childManager, childTraverser);
} else {
key = args.pool.toKey(d.getArtifact(), repositories);
}
List<RemoteRepository> repos;
ArtifactRepository repo = rangeResult.getRepository(version);
if (repo instanceof RemoteRepository) {
repos = Collections.singletonList((RemoteRepository) repo);
} else if (repo == null) {
repos = repositories;
} else {
repos = Collections.emptyList();
}
GraphNode child = args.pool.getNode(key);
if (child == null) {
child = new GraphNode();
child.setAliases(descriptorResult.getAliases());
child.setRepositories(repos);
args.pool.putNode(key, child);
} else {
recurse = false;
if (repos.size() < child.getRepositories().size()) {
child.setRepositories(repos);
}
}
GraphEdge edge = new GraphEdge(child);
edge.setDependency(d);
edge.setScope(d.getScope());
edge.setPremanagedScope(premanagedScope);
edge.setPremanagedVersion(premanagedVersion);
edge.setRelocations(relocations);
edge.setVersionConstraint(rangeResult.getVersionConstraint());
edge.setVersion(version);
edge.setRequestContext(args.result.getRequest().getRequestContext());
node.getOutgoingEdges().add(edge);
if (recurse) {
args.edges.push(edge);
process(args, descriptorResult.getDependencies(), childRepos, childSelector, childManager, childTraverser);
args.edges.pop();
}
}
break;
}
}
}
use of org.sonatype.aether.graph.Dependency in project sonatype-aether by sonatype.
the class DefaultDependencyCollectorTest method testPartialResultOnError.
@Test
public void testPartialResultOnError() throws IOException {
DependencyNode root = parser.parse("expectedPartialSubtreeOnError.txt");
Dependency dependency = root.getDependency();
CollectRequest request = new CollectRequest(dependency, Arrays.asList(repository));
CollectResult result;
try {
result = collector.collectDependencies(session, request);
fail("expected exception ");
} catch (DependencyCollectionException e) {
result = e.getResult();
assertSame(request, result.getRequest());
assertNotNull(result.getExceptions());
assertEquals(1, result.getExceptions().size());
assertTrue(result.getExceptions().get(0) instanceof ArtifactDescriptorException);
assertEqualSubtree(root, result.getRoot());
}
}
use of org.sonatype.aether.graph.Dependency in project sonatype-aether by sonatype.
the class DefaultDependencyCollectorTest method testManagedVersionScope.
@Test
public void testManagedVersionScope() throws IOException, DependencyCollectionException {
DependencyNode root = parser.parseLiteral("managed:aid:ext:ver");
Dependency dependency = root.getDependency();
CollectRequest request = new CollectRequest(dependency, Arrays.asList(repository));
session.setDependencyManager(new ClassicDependencyManager());
CollectResult result = collector.collectDependencies(session, request);
assertEquals(0, result.getExceptions().size());
DependencyNode newRoot = result.getRoot();
assertEquals(dependency, dep(newRoot));
assertEquals(dependency.getArtifact(), dep(newRoot).getArtifact());
assertEquals(1, newRoot.getChildren().size());
DependencyNode expect = parser.parseLiteral("gid:aid:ext:ver:compile");
assertEquals(dep(expect), dep(newRoot, 0));
assertEquals(1, path(newRoot, 0).getChildren().size());
expect = parser.parseLiteral("gid:aid2:ext:managedVersion:managedScope");
assertEquals(dep(expect), dep(newRoot, 0, 0));
}
Aggregations