Programmatically get Maven dependency graph

Given the Maven artifact (groupId: artifactId: version), how can I programmatically request its dependencies? (I don't really need to retrieve any artifacts, just dependency information.)

Edit to add I would like to do this outside of the Maven plugin, and I would like to create a dependency graph.

+4
source share
3 answers

If you use the maven plugin (ex: extend abstractMojo), you can do the following:

/** * @parameter expression="${project}" */ private org.apache.maven.project.MavenProject mavenProject; List<org.apache.maven.model.Dependency> depmgtdeps = mavenProject.getDependencyManagement().getDependencies(); 

This will give you the actual dependency objects that it detects. The MavenProject class also contains many other methods for reading various things related to pom. However, I do not think this works outside the plugin, or at least I never tried to do it.

+2
source
+1
source

The following groovy script uses ivy to resolve dependencies

 import groovy.xml.NamespaceBuilder // Main program // ============ def ant = new AntBuilder() def ivy = NamespaceBuilder.newInstance(ant, "antlib:org.apache.ivy.ant") ivy.resolve( inline:true, keep:true, conf:"default", organisation:"org.springframework", module:"spring-core", revision:"3.1.1.RELEASE", ) ivy.report(toDir:"reports") 

Creates an HTML report and graphic file:

 |-- report.groovy |-- reports | |-- ivy-report.css | |-- org.springframework-spring-core-caller-default.graphml | `-- org.springframework-spring-core-caller-default.html 
+1
source

All Articles