Reputation: 33
One of the requirements for a program I am working on is that I need to be able to look through the Maven dependencies of several artifacts in a repository so I can create dependency graphs for them. While it is obvious that Maven and Eclipse Aether can do this (as a huge part of Maven is getting dependencies), I'm having a really tough time figuring out how to do it in a Java program.
Any suggestions?
Upvotes: 1
Views: 2319
Reputation: 33
After looking around at various different examples and code, I cobbled together this, which seems to work:
public List<Artifact> fetchArtifactDependencies(final RepositorySystemSession session,
final Artifact artifact,
final DependencyFilter dependencyFilter)
throws RepositoryException {
final CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, "compile"));
collectRequest.addRepository([repository]);
final DependencyNode node = repositorySystem.collectDependencies(session, collectRequest)
.getRoot();
final DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot(node);
dependencyRequest.setFilter(dependencyFilter);
final DependencyResult dependencyResult = repositorySystem.resolveDependencies(session,
dependencyRequest);
final List<ArtifactResult> artifactResults = dependencyResult.getArtifactResults();
final List<Artifact> results = new ArrayList<>(artifactResults.size());
CollectionUtils.collect(artifactResults, new Transformer<ArtifactResult, Artifact>() {
@Override
public Artifact transform(final ArtifactResult input) {
return input.getArtifact();
}
}, results);
return results;
}
Upvotes: 0
Reputation: 97359
There several way to accomplish this is either using the IDE like Eclipse...or you can use the maven-dependency-plugin just print out into the console...
mvn dependency:tree
Upvotes: 0
Reputation: 32905
It seems that Aether can help, according to the documentation. There is even an example that demonstrates how to use Aether to collect the transitive dependencies of an artifact. Combining that with the Maven API example here, I think you can get where you want.
Upvotes: 1