Skip to content

Commit

Permalink
New command: tree-find
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Jun 11, 2024
1 parent 4c729e7 commit 4e9e23c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ boolean resolveTransitive(
boolean tree(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, boolean verbose, Output output)
throws Exception;

boolean treeFind(
ResolutionScope resolutionScope,
ResolutionRoot resolutionRoot,
boolean verbose,
ArtifactMatcher artifactMatcher,
Output output)
throws Exception;

// Search API related commands: they target one single RemoteRepository

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.installation.InstallRequest;
Expand All @@ -77,6 +78,7 @@
import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.aether.util.artifact.ArtifactIdUtils;
import org.eclipse.aether.util.artifact.SubArtifact;
import org.eclipse.aether.util.graph.visitor.PathRecordingDependencyVisitor;
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;
import org.eclipse.aether.util.listener.ChainedRepositoryListener;
Expand Down Expand Up @@ -624,6 +626,49 @@ public boolean tree(
}
}

@Override
public boolean treeFind(
ResolutionScope resolutionScope,
ResolutionRoot resolutionRoot,
boolean verbose,
ArtifactMatcher artifactMatcher,
Output output) {
try {
output.verbose("Loading root of: {}", resolutionRoot.getArtifact());
ResolutionRoot root = toolboxResolver.loadRoot(resolutionRoot);
output.verbose("Collecting graph of: {}", resolutionRoot.getArtifact());
CollectResult collectResult = toolboxResolver.collect(
resolutionScope,
root.getArtifact(),
root.getDependencies(),
root.getManagedDependencies(),
verbose);
PathRecordingDependencyVisitor pathRecordingDependencyVisitor =
new PathRecordingDependencyVisitor(new DependencyFilter() {
@Override
public boolean accept(DependencyNode node, List<DependencyNode> parents) {
return node.getArtifact() != null && artifactMatcher.test(node.getArtifact());
}
});
collectResult.getRoot().accept(pathRecordingDependencyVisitor);
if (!pathRecordingDependencyVisitor.getPaths().isEmpty()) {
output.normal("Paths");
for (List<DependencyNode> path : pathRecordingDependencyVisitor.getPaths()) {
String indent = "";
for (DependencyNode node : path) {
output.normal("{}-> {}", indent, node.getArtifact());
indent += " ";
}
}
} else {
output.normal("No paths found.");
}
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Map<String, RemoteRepository> getKnownSearchRemoteRepositories() {
return knownSearchRemoteRepositories;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.plugin.mp;

import eu.maveniverse.maven.toolbox.plugin.MPMojoSupport;
import eu.maveniverse.maven.toolbox.shared.Output;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Collects paths to matched artifact, if exists.
*/
@Mojo(name = "tree-find", threadSafe = true)
public class TreeFindMojo extends MPMojoSupport {
/**
* The resolution scope to display, accepted values are "runtime", "compile", "test", etc.
*/
@Parameter(property = "scope", defaultValue = "runtime", required = true)
private String scope;

/**
* The artifact matcher spec.
*/
@Parameter(property = "artifactMatcherSpec", required = true)
private String artifactMatcherSpec;

/**
* Set it {@code true} for verbose tree.
*/
@Parameter(property = "verboseTree", defaultValue = "false", required = true)
private boolean verboseTree;

@Override
protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws Exception {
return toolboxCommando.treeFind(
ResolutionScope.parse(scope),
projectAsResolutionRoot(),
verboseTree,
toolboxCommando.parseArtifactMatcherSpec(artifactMatcherSpec),
output);
}
}

0 comments on commit 4e9e23c

Please sign in to comment.