如何使用Maven的Java API访问Maven Artifact POM? [英] How can I access Maven Artifact POM using Maven's Java API?
问题描述
我正在尝试从项目的根目录开始检索整个依赖关系树及其pom.我从文件系统中已经存在的POM开始,但是我不确定如何从存储库中检索依赖项poms.
I am attempting to retrieve the entire dependency tree and their poms starting at the root of the project. I am starting with a POM already existing in my file system, but I'm not sure how to retrieve the dependency poms from the repository.
我正在使用以下代码访问依赖项列表.从列表中,我可以获得有关工件的所有信息.我只是不确定如何访问存储库.
I am using the following code to access the dependency list. From the list I have all the information on the artifacts. I'm just not sure how to access the repository.
FileReader reader = null;
Model model = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
File pomfile = new File("pom.xml");
model.setPomFile(pomfile);
MavenProject project = new MavenProject(model);
List<Dependency> deps = project.getDependencies();
// Get dependency details
for (Dependency d: deps) {
System.out.print(d.getArtifactId());
System.out.print(":");
System.out.println(d.getVersion());
}
推荐答案
实际上,您已经接近阅读依赖项.所缺少的只是:
Actually you were pretty close to reading the dependencies. All that was missing was:
Model model = mavenreader.read(new FileReader(pomFile));
完整示例:
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
File pomfile = new File("pom.xml");
Model model = mavenreader.read(new FileReader(pomFile));
List<Dependency> deps = model.getDependencies();
for (Dependency d: deps) {
System.out.print(d.getArtifactId());
}
这不会为您提供依赖关系树,但是您可以为找到的依赖关系重复它.
This doesn't get you a dependency tree but you can repeat it for the dependencies you find.
这篇关于如何使用Maven的Java API访问Maven Artifact POM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!