Mojo发展。如何遍历托管依赖项?

时间:2014-02-21 14:11:56

标签: java maven maven-plugin pom.xml maven-dependency-plugin

我们正在开发一个框架,并希望发布a BOM(bill of material) like Spring 4 does。在此主pom.xml中,我们组件的所有版本都列为托管依赖项。因为我们有很多组件,所以我们想检查我们的其他组件中是否列出了两个不同版本的组件。

在内部使用BOM或创建第二个内部BOM不是解决方案,因为这会导致循环依赖。

现在,我的想法是创建一个maven插件,遍历所有依赖项并查找重复项。我设法用" normal"依赖项,但不适用于托管依赖项。

ArtifactFilter myArtifactFilter = new MyArtifactFilter();
rootNode = dependencyTreeBuilder.buildDependencyTree(project, localRepository, artifactFactory, artifactMetadataSource, null, collector);

CollectingDependencyNodeVisitor cdnv = new CollectingDependencyNodeVisitor();
DependencyNodeFilter adnf = new ArtifactDependencyNodeFilter(myArtifactFilter);
DependencyNodeVisitor dnv = new FilteringDependencyNodeVisitor(cdnv, adnf);

rootNode.accept(dnv);

List<DependencyNode> nodes = cdnv.getNodes();

for (int i = 0; i < nodes.size(); i++) {
    getLog().info(i + ": " + nodes.get(i).toNodeString());
}

有谁知道如何获取托管依赖项?

1 个答案:

答案 0 :(得分:1)

我这样解决了:

List<DependencyNode> allNodes = new ArrayList<DependencyNode>();
DependencyManagement dependencyManagement = project.getDependencyManagement();
List<Dependency> dependencies = dependencyManagement.getDependencies();

getLog().info("found the following managed dependencies:");
for (Dependency dependency : dependencies) {
    getLog().info(dependency.toString());
    String groupId = dependency.getGroupId();
    String artifactId = dependency.getArtifactId();
    String version = dependency.getVersion();
    String scope = dependency.getScope();
    String type = dependency.getType();
    Artifact artifact = artifactFactory.createArtifact(groupId, artifactId, version, scope, type);
    MavenProject buildFromRepository = mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository);
    allNodes.addAll(getDependencies(buildFromRepository));
}

问题中的代码移入getDependencies()方法,该方法返回依赖项列表。