Domanda

We are developing a framework and want to publish a BOM(bill of material) like Spring 4 does. In this main pom.xml, all versions of our components are listed as managed dependencies. Because we have a lot of components, we want to check if any of our components is listed with two differing versions in our other components.

Using the BOM internally or creating a second internal BOM is not a solution because that would result in circular dependencies.

Now, my idea is to create a maven plugin that traverses all dependencies and looks for duplicates. I managed to do that with "normal" dependencies, but not for managed dependencies.

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());
}

Does anyone know how to get the managed dependencies?

È stato utile?

Soluzione

I solved it that way:

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));
}

the code from the question moved into the getDependencies() method which returns a List of dependencies.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top