public static List<Artifact> getAllLibraries(MavenProject project, RepositorySystemSession repositorySystemSession, ProjectDependenciesResolver projectDependenciesResolver) throws MojoExecutionException { DefaultDependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest(project, repositorySystemSession); DependencyResolutionResult dependencyResolutionResult; try { dependencyResolutionResult = projectDependenciesResolver.resolve(dependencyResolutionRequest); } catch (DependencyResolutionException ex) { throw new MojoExecutionException(ex.getMessage(), ex); } List<Artifact> artifacts = new LinkedList<Artifact>(); if (dependencyResolutionResult.getDependencyGraph() != null && !dependencyResolutionResult.getDependencyGraph().getChildren().isEmpty()) { RepositoryUtils.toArtifacts(artifacts, dependencyResolutionResult.getDependencyGraph().getChildren(), Collections.singletonList(project.getArtifact().getId()), null); } return artifacts; }
protected List<File> resolveArtifacts(MavenProject mavenProject, Collection<String> scopes, DependencyFilter filter) throws MojoExecutionException { try { DefaultDependencyResolutionRequest request = new DefaultDependencyResolutionRequest(mavenProject, repoSession); if(filter != null) { request.setResolutionFilter(new AndDependencyFilter(new ScopeDependencyFilter(scopes, Collections.<String>emptyList()), filter)); } else { request.setResolutionFilter(new ScopeDependencyFilter(scopes, Collections.<String>emptyList())); } DependencyResolutionResult result = projectDependenciesResolver.resolve(request); List<org.eclipse.aether.graph.Dependency> dependencies = result.getDependencies(); final List<File> files = new ArrayList<File>(); for(org.eclipse.aether.graph.Dependency d : dependencies) { org.eclipse.aether.artifact.Artifact artifact = d.getArtifact(); File file = artifact.getFile(); if(file != null) { String name = file.getName(); if(!name.contains("xml-apis") && !name.contains("commons-logging")) files.add(file); } } return files; } catch (DependencyResolutionException e) { throw new MojoExecutionException("Dependency resolution failure: " + e.getMessage(), e); } }
private void addTransitiveDependencies(final File distroFolder, final Collection<String> includedArtifacts, final Dependency dependency) { final DependencyResolutionRequest request = new DefaultDependencyResolutionRequest(); request.setMavenProject(new MavenProject() {{ getDependencies().add(dependency); }}); request.setRepositorySession(session); try { dependenciesResolver.resolve(request).getDependencyGraph().accept(new DependencyVisitor() { @Override public boolean visitEnter(final DependencyNode node) { return true; } @Override public boolean visitLeave(final DependencyNode node) { final org.eclipse.aether.artifact.Artifact artifact = node.getArtifact(); if (artifact != null && !includedArtifacts.contains(artifact.getArtifactId())) { addLib(distroFolder, artifact.getFile()); } return true; } }); } catch (final DependencyResolutionException e) { throw new IllegalStateException(e.getMessage(), e); } }