DefaultDependencyCollector implements DependencyCollector, Service { 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; } DefaultDependencyCollector();  DefaultDependencyCollector( Logger logger, RemoteRepositoryManager remoteRepositoryManager,
                                       ArtifactDescriptorReader artifactDescriptorReader,
                                       VersionRangeResolver versionRangeResolver ); void initService( ServiceLocator locator ); DefaultDependencyCollector setLogger( Logger logger ); DefaultDependencyCollector setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ); DefaultDependencyCollector setArtifactDescriptorReader( ArtifactDescriptorReader artifactDescriptorReader ); DefaultDependencyCollector setVersionRangeResolver( VersionRangeResolver versionRangeResolver ); CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request );  }
@Test public void testSimpleCollection() throws IOException, DependencyCollectionException { DependencyNode root = parser.parseLiteral( "gid:aid:ext:ver" ); Dependency dependency = root.getDependency(); CollectRequest request = new CollectRequest( dependency, Arrays.asList( repository ) ); CollectResult result = collector.collectDependencies( session, request ); assertEquals( 0, result.getExceptions().size() ); DependencyNode newRoot = result.getRoot(); Dependency newDependency = newRoot.getDependency(); assertEquals( dependency, newDependency ); assertEquals( dependency.getArtifact(), newDependency.getArtifact() ); assertEquals( 1, newRoot.getChildren().size() ); DependencyNode expect = parser.parseLiteral( "gid:aid2:ext:ver:compile" ); assertEquals( expect.getDependency(), newRoot.getChildren().get( 0 ).getDependency() ); }
@Test public void testMissingDependencyDescription() throws IOException { DependencyNode root = parser.parseLiteral( "missing:description:ext:ver" ); CollectRequest request = new CollectRequest( root.getDependency(), Arrays.asList( repository ) ); try { collector.collectDependencies( session, request ); fail( "expected exception" ); } catch ( DependencyCollectionException e ) { CollectResult result = e.getResult(); assertSame( request, result.getRequest() ); assertNotNull( result.getExceptions() ); assertEquals( 1, result.getExceptions().size() ); assertTrue( result.getExceptions().get( 0 ) instanceof ArtifactDescriptorException ); assertEquals( request.getRoot(), result.getRoot().getDependency() ); } }
@Test public void testDuplicates() throws IOException, DependencyCollectionException { DependencyNode root = parser.parseLiteral( "duplicate:transitive:ext:dependency" ); Dependency dependency = root.getDependency(); CollectRequest request = new CollectRequest( dependency, Arrays.asList( repository ) ); CollectResult result = collector.collectDependencies( session, request ); assertEquals( 0, result.getExceptions().size() ); DependencyNode newRoot = result.getRoot(); Dependency newDependency = newRoot.getDependency(); assertEquals( dependency, newDependency ); assertEquals( dependency.getArtifact(), newDependency.getArtifact() ); assertEquals( 2, newRoot.getChildren().size() ); DependencyNode expect = parser.parseLiteral( "gid:aid:ext:ver:compile" ); Dependency dep = expect.getDependency(); assertEquals( dep, dep( newRoot, 0 ) ); expect = parser.parseLiteral( "gid:aid2:ext:ver:compile" ); dep = expect.getDependency(); assertEquals( dep, dep( newRoot, 1 ) ); assertEquals( dep, dep( newRoot, 0, 0 ) ); assertEquals( dep( newRoot, 1 ), dep( newRoot, 0, 0 ) ); }
@Test public void testEqualSubtree() throws IOException, DependencyCollectionException { DependencyNode root = parser.parse( "expectedSubtreeComparisonResult.txt" ); Dependency dependency = root.getDependency(); CollectRequest request = new CollectRequest( dependency, Arrays.asList( repository ) ); CollectResult result = collector.collectDependencies( session, request ); assertEqualSubtree( root, result.getRoot() ); }
@Test public void testCyclicDependencies() throws Exception { DependencyNode root = parser.parse( "cycle.txt" ); CollectRequest request = new CollectRequest( root.getDependency(), Arrays.asList( repository ) ); CollectResult result = collector.collectDependencies( session, request ); assertEqualSubtree( root, result.getRoot() ); }
@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() ); } }
@Test public void testCollectMultipleDependencies() throws IOException, DependencyCollectionException { DependencyNode root1 = parser.parseLiteral( "gid:aid:ext:ver:compile" ); DependencyNode root2 = parser.parseLiteral( "gid:aid2:ext:ver:compile" ); List<Dependency> dependencies = Arrays.asList( root1.getDependency(), root2.getDependency() ); CollectRequest request = new CollectRequest( dependencies, null, Arrays.asList( repository ) ); CollectResult result = collector.collectDependencies( session, request ); assertEquals( 0, result.getExceptions().size() ); assertEquals( 2, result.getRoot().getChildren().size() ); assertEquals( root1.getDependency(), dep( result.getRoot(), 0 ) ); assertEquals( 1, path( result.getRoot(), 0 ).getChildren().size() ); assertEquals( root2.getDependency(), dep( result.getRoot(), 0, 0 ) ); assertEquals( 0, path( result.getRoot(), 1 ).getChildren().size() ); assertEquals( root2.getDependency(), dep( result.getRoot(), 1 ) ); }
@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 ) ); }