use of org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor in project maven-dependency-plugin by apache.
the class TreeMojo method serializeDependencyTree.
/**
* Serializes the specified dependency tree to a string.
*
* @param theRootNode the dependency tree root node to serialize
* @return the serialized dependency tree
*/
private String serializeDependencyTree(DependencyNode theRootNode) {
StringWriter writer = new StringWriter();
DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor(writer);
// TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
visitor = new BuildingDependencyNodeVisitor(visitor);
DependencyNodeFilter filter = createDependencyNodeFilter();
if (filter != null) {
CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(collectingVisitor, filter);
theRootNode.accept(firstPassVisitor);
DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
visitor = new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
}
theRootNode.accept(visitor);
return writer.toString();
}
use of org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor in project component-runtime by Talend.
the class ArtifactTransformer method hasTransformedResource.
@Override
public boolean hasTransformedResource() {
artifacts = new ArrayList<>();
final ArtifactFilter filter = getFilter();
if (userArtifacts != null && !userArtifacts.isEmpty()) {
final ArtifactResolver resolver;
final DependencyGraphBuilder graphBuilder;
final ProjectBuilder projectBuilder;
final PlexusContainer container = session.getContainer();
try {
resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
} catch (final ComponentLookupException e) {
throw new IllegalArgumentException(e);
}
artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
try {
final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {
{
setExtension(type);
}
});
final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
if (includeTransitiveDependencies) {
final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
try {
final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
try {
return resolver.resolveArtifact(request, a).getArtifact();
} catch (final ArtifactResolverException e) {
throw new IllegalStateException(e);
}
}));
} catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
throw new IllegalStateException(e);
}
}
return Stream.of(artifact);
} catch (final ArtifactResolverException e) {
throw new IllegalArgumentException(e);
}
}).collect(toSet()));
}
if (includeProjectComponentDependencies) {
// this is a weird category of projects but can exist
final MavenProject project = session.getCurrentProject();
if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
project.setArtifactFilter(filter);
try {
artifacts.addAll(project.getArtifacts());
} finally {
// shade plugin uses it OOTB so reset it for the end of the execution (in case
// another transformer needs it)
project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
}
}
}
return !artifacts.isEmpty();
}
use of org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor in project felix by apache.
the class AbstractDependencyFilter method filteredDependencies.
private void filteredDependencies(final ArtifactFilter artifactFilter, Collection<Artifact> filteredDependencies) {
CollectingDependencyNodeVisitor collectingDependencyNodeVisitor = new CollectingDependencyNodeVisitor();
final Artifact rootArtifact = m_dependencyGraph.getArtifact();
ArtifactFilter filter = new ArtifactFilter() {
public boolean include(Artifact artifact) {
return artifact.equals(rootArtifact) || artifactFilter.include(artifact);
}
};
DependencyNodeFilter dependencyNodeFilter = new ArtifactDependencyNodeFilter(filter);
dependencyNodeFilter = new TrimmingDependencyNodeFilter(dependencyNodeFilter);
DependencyNodeVisitor dependencyNodeVisitor = new FilteringDependencyNodeVisitor(collectingDependencyNodeVisitor, dependencyNodeFilter);
dependencyNodeVisitor = new BuildingDependencyNodeVisitor(dependencyNodeVisitor);
m_dependencyGraph.accept(dependencyNodeVisitor);
List<DependencyNode> dependencyNodes = collectingDependencyNodeVisitor.getNodes();
Set<String> ids = new LinkedHashSet<String>(dependencyNodes.size());
for (DependencyNode dependencyNode : dependencyNodes) {
Artifact artifact = dependencyNode.getArtifact();
String id = artifact.getId();
ids.add(id);
}
for (Iterator<Artifact> iterator = filteredDependencies.iterator(); iterator.hasNext(); ) {
Artifact artifact = iterator.next();
String id = artifact.getId();
if (!ids.contains(id)) {
iterator.remove();
}
}
}
use of org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor in project maven-plugins by apache.
the class TreeMojo method serializeDependencyTree.
/**
* Serializes the specified dependency tree to a string.
*
* @param rootNode the dependency tree root node to serialize
* @return the serialized dependency tree
*/
private String serializeDependencyTree(DependencyNode rootNode) {
StringWriter writer = new StringWriter();
DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor(writer);
// TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
visitor = new BuildingDependencyNodeVisitor(visitor);
DependencyNodeFilter filter = createDependencyNodeFilter();
if (filter != null) {
CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(collectingVisitor, filter);
rootNode.accept(firstPassVisitor);
DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
visitor = new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
}
rootNode.accept(visitor);
return writer.toString();
}
Aggregations