2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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 License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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("maven")
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,
58 ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index) {
59 this.localRepository = localRepository;
60 this.artifactFactory = artifactFactory;
61 this.artifactMetadataSource = artifactMetadataSource;
62 this.artifactCollector = artifactCollector;
64 this.treeBuilder = treeBuilder;
67 public boolean shouldExecuteOnProject(Project project) {
71 public void analyse(final Project project, final SensorContext context) {
73 DependencyNode root = treeBuilder.buildDependencyTree(project.getPom(), localRepository, artifactFactory, artifactMetadataSource, null, artifactCollector);
75 DependencyNodeVisitor visitor = new BuildingDependencyNodeVisitor(new DependencyNodeVisitor() {
76 public boolean visit(DependencyNode node) {
80 public boolean endVisit(DependencyNode node) {
81 if (node.getParent() != null && node.getParent() != node) {
82 saveDependency(node, context);
88 // mode verbose OFF : do not show the same lib many times
89 DependencyNodeFilter filter = StateDependencyNodeFilter.INCLUDED;
91 CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
92 DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(collectingVisitor, filter);
93 root.accept(firstPassVisitor);
95 DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
96 visitor = new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
100 } catch (DependencyTreeBuilderException e) {
101 throw new SonarException("Can not load the graph of dependencies of the project " + project.getKey(), e);
105 protected void saveDependency(DependencyNode node, SensorContext context) {
106 Resource from = (node.getParent().getParent() == null) ? index.getProject() : toResource(node.getParent().getArtifact(), context);
107 Resource to = toResource(node.getArtifact(), context);
108 Dependency dependency = new Dependency(from, to);
109 dependency.setUsage(node.getArtifact().getScope());
110 dependency.setWeight(1);
111 context.saveDependency(dependency);
114 protected static Resource toResource(Artifact artifact, SensorContext context) {
115 Project project = Project.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId());
116 Resource result = context.getResource(project);
117 if (result == null || !((Project) result).getAnalysisVersion().equals(artifact.getBaseVersion())) {
118 Library lib = new Library(project.getKey(), artifact.getBaseVersion());
119 context.saveResource(lib);
120 result = context.getResource(lib);
126 public String toString() {
127 return "Maven dependencies";