public static void main(String[] args) throws Exception { final String uri = "com.carrotgarden.osgi:carrot-osgi-anno-scr-make:jar:1.1.3"; final Artifact artifact = new DefaultArtifact(uri); Dependency dependency = new Dependency(artifact, "compile"); CollectRequest collectRequest = new CollectRequest(dependency, null); RepositorySystem system = UnitHelp.newSystem(); MavenRepositorySystemSession session = UnitHelp.newSession(system); session.setOffline(true); Collection<String> scopeIncluded = new ArrayList<String>(); Collection<String> scopeExcluded = new ArrayList<String>(); scopeIncluded.add("provided"); scopeExcluded.add("test"); session.setDependencySelector( // new AndDependencySelector( // new OptionalDependencySelector(), // new ScopeDependencySelector(scopeIncluded, scopeExcluded), // new ExclusionDependencySelector()) // ); CollectResult collectResult = system.collectDependencies(session, collectRequest); DependencyNode collectNode = collectResult.getRoot(); final DependencyRequest dependencyRequest = new DependencyRequest(collectNode, null); final DependencyResult result = system.resolveDependencies(session, dependencyRequest); final DependencyNode resolveNode = result.getRoot(); final PreorderNodeListGenerator generator = new PreorderNodeListGenerator(); resolveNode.accept(generator); List<Artifact> list = generator.getArtifacts(true); for (Artifact item : list) { System.out.println("item = " + item); } }
public void execute() throws MojoExecutionException, MojoFailureException { try { ArtifactDescriptorResult artifactDescriptor = repoSystem.readArtifactDescriptor( repoSession, new ArtifactDescriptorRequest(new DefaultArtifact(artifact), projectRepos, null)); CollectRequest collectRequest = new CollectRequest(); collectRequest.setDependencies(artifactDescriptor.getDependencies()); collectRequest.setManagedDependencies(artifactDescriptor.getManagedDependencies()); collectRequest.setRepositories(projectRepos); CollectResult dependencies = repoSystem.collectDependencies(repoSession, collectRequest); StringWriter writer = new StringWriter(); NodeFormatter nodeFormatter = new DefaultNodeFormatter(artifactDescriptor.getArtifact().toString()); dependencies .getRoot() .accept( new SerializingDependencyVisitor( writer, SerializingDependencyVisitor.STANDARD_TOKENS, nodeFormatter)); try { if (outputFile == null) { log(writer.toString(), getLog()); } else { write(writer.toString(), outputFile); getLog().info("Wrote dependency tree to: " + outputFile); } } catch (IOException e) { throw new MojoExecutionException("Failed to write dependencies to file", e); } } catch (ArtifactDescriptorException e) { throw new MojoExecutionException("Failed to read artifact descriptor", e); } catch (DependencyCollectionException e) { throw new MojoExecutionException("Failed to collect dependencies", e); } }
@Override public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request) throws DependencyCollectionException { debugf("collectDependencies %s", request); validateSession(session); // delegate has been wired up to come back to us, so this must be a real // implementation DependencyCollectionException originalException = null; // TODO: work in progress, we need to aggregate all // try JPP local repo if (useJpp) { // use maven as much as possible final RepositorySystemSession alternateSession = openJpp(session); try { final CollectRequest alternateRequest = new CollectRequest( request.getRoot(), request.getDependencies(), singletonList(fossRepository)) .setManagedDependencies(request.getManagedDependencies()) .setTrace(request.getTrace()); final CollectResult result = dependencyCollector.collectDependencies(alternateSession, alternateRequest); if (result.getExceptions().isEmpty() && result.getRoot() != null) { logger.warn("collectDependencies: result from JPP " + result); // logger.warn("TODO"); return result; } } catch (DependencyCollectionException e) { logger.debug("JPP collect dependencies of " + request + " failed", e); originalException = e; } } // try FOSS local repo { try { final CollectRequest alternateRequest = new CollectRequest() .setRoot(request.getRoot()) .setDependencies(request.getDependencies()) .setManagedDependencies(request.getManagedDependencies()) .setRequestContext(request.getRequestContext()) .setTrace(request.getTrace()) .setRepositories(singletonList(fossRepository)); final CollectResult result = dependencyCollector.collectDependencies(session, alternateRequest); logger.debug("collectDependencies: result = " + result); if (result.getExceptions().isEmpty()) { return result; } } catch (DependencyCollectionException e) { logger.warn("collect dependencies failed ", e); if (originalException == null) { originalException = e; } } } if (originalException != null) { throw originalException; } throw new RuntimeException( "NYI: org.fedoraproject.maven.repository.internal." + "FossRepositorySystem.collectDependencies"); }