1

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?

  • the "brute force" method at the moment would be to open up the pom myself and scrounge for dependencies in the xml file, but since Maven already can do this for me, seems like a waste of time. Just can't figure out how to get Maven to do it in the java code. – Dragonfiremalus Jun 17 '15 at 15:19

3 Answers3

1

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.

Community
  • 1
  • 1
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Aether moved to github: https://github.com/eclipse/aether-demo/blob/master/aether-demo-snippets/src/main/java/org/eclipse/aether/examples/GetDependencyTree.java – Stefan Höltker Nov 06 '19 at 10:55
  • 1
    The entire Aether project has been archived by Eclipse, meaning it's no longer under active development. – E-Riz Nov 06 '19 at 14:46
  • Thanks @E-Riz i learned that it is now under Apache with the name "maven-resolver" https://github.com/apache/maven-resolver/blob/master/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/GetDependencyTree.java – Stefan Höltker Nov 07 '19 at 10:26
0

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
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • The issue is I need my program to grab the dependencies for several different ones and do stuff with what's returned. I could attempt the console command using java Runtime stuff, but then I'd be left searching through the output for what I want. Not fun. – Dragonfiremalus Jun 17 '15 at 17:21
0

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;
}