2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.design.batch;
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.resolver.ArtifactCollector;
27 import org.apache.maven.shared.dependency.tree.DependencyNode;
28 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
29 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
30 import org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter;
31 import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
32 import org.apache.maven.shared.dependency.tree.filter.StateDependencyNodeFilter;
33 import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor;
34 import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
35 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
36 import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
37 import org.sonar.api.batch.SupportedEnvironment;
38 import org.sonar.api.batch.Sensor;
39 import org.sonar.api.batch.SensorContext;
40 import org.sonar.api.batch.SonarIndex;
41 import org.sonar.api.design.Dependency;
42 import org.sonar.api.resources.Library;
43 import org.sonar.api.resources.Project;
44 import org.sonar.api.resources.Resource;
45 import org.sonar.api.utils.SonarException;
47 @SupportedEnvironment({ "maven2", "maven3" })
48 public class MavenDependenciesSensor implements Sensor {
50 private ArtifactRepository localRepository;
51 private ArtifactFactory artifactFactory;
52 private ArtifactMetadataSource artifactMetadataSource;
53 private ArtifactCollector artifactCollector;
54 private DependencyTreeBuilder treeBuilder;
55 private SonarIndex index;
57 public MavenDependenciesSensor(ArtifactRepository localRepository, ArtifactFactory artifactFactory, ArtifactMetadataSource artifactMetadataSource, ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index) {
58 this.localRepository = localRepository;
59 this.artifactFactory = artifactFactory;
60 this.artifactMetadataSource = artifactMetadataSource;
61 this.artifactCollector = artifactCollector;
63 this.treeBuilder = treeBuilder;
66 public boolean shouldExecuteOnProject(Project project) {
70 public void analyse(final Project project, final SensorContext context) {
72 DependencyNode root = treeBuilder.buildDependencyTree(project.getPom(), localRepository, artifactFactory, artifactMetadataSource, null, artifactCollector);
74 DependencyNodeVisitor visitor = new BuildingDependencyNodeVisitor(new DependencyNodeVisitor() {
75 public boolean visit(DependencyNode node) {
79 public boolean endVisit(DependencyNode node) {
80 if (node.getParent() != null && node.getParent() != node) {
81 saveDependency(node, context);
87 // mode verbose OFF : do not show the same lib many times
88 DependencyNodeFilter filter = StateDependencyNodeFilter.INCLUDED;
90 CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
91 DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(collectingVisitor, filter);
92 root.accept(firstPassVisitor);
94 DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
95 visitor = new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
99 } catch (DependencyTreeBuilderException e) {
100 throw new SonarException("Can not load the graph of dependencies of the project " + project.getKey(), e);
104 protected void saveDependency(DependencyNode node, SensorContext context) {
105 Resource from = (node.getParent().getParent() == null) ? index.getProject() : toResource(node.getParent().getArtifact(), context);
106 Resource to = toResource(node.getArtifact(), context);
107 Dependency dependency = new Dependency(from, to);
108 dependency.setUsage(node.getArtifact().getScope());
109 dependency.setWeight(1);
110 context.saveDependency(dependency);
113 protected static Resource toResource(Artifact artifact, SensorContext context) {
114 Project project = Project.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId());
115 Resource result = context.getResource(project);
116 if (result == null || !((Project) result).getAnalysisVersion().equals(artifact.getBaseVersion())) {
117 Library lib = new Library(project.getKey(), artifact.getBaseVersion());
118 context.saveResource(lib);
119 result = context.getResource(lib);
125 public String toString() {
126 return "Maven dependencies";