Programmatically retrieve Maven dependency graph

落爺英雄遲暮 提交于 2019-12-07 09:28:19

问题


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

Edit to add I'd like to do this outside of a Maven plug-in, and I'd like to build up a dependency graph.


回答1:


If you're using a maven plugin (ie: 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();

That will give you the actual dependency objects that it detects. The MavenProject class has a bunch of other methods as well for reading various pom related things. However, I don't believe this works outside a plugin, or at least, I've never tried to do it.




回答2:


I found these two links helpful..

  1. A Maven Plugin for retrieving the dependencies
  2. Using Eclipse Plugin



回答3:


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")

Generates a HTML report and a graphml file:

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


来源:https://stackoverflow.com/questions/11189817/programmatically-retrieve-maven-dependency-graph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!