问题描述
鉴于Maven工件(groupId:artifactId:version),我如何以编程方式查询其依赖项? (我不需要实际检索任何工件,只需要检索相关性信息。)
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.)
编辑添加我想在外面执行此操作一个Maven插件,我想建立一个依赖图。
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.
推荐答案
如果你正在使用一个maven插件(即:扩展AbstractMojo),您可以执行以下操作:
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();
这将为您提供它检测到的实际依赖项对象。 MavenProject类还有许多其他方法可用于读取各种与pom相关的内容。但是,我不认为这可以在插件之外使用,或者至少,我从未尝试过这样做。
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.
这篇关于以编程方式检索Maven依赖关系图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!