问题
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..
- A Maven Plugin for retrieving the dependencies
- 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