]> source.dussan.org Git - sonarqube.git/blob
088b3d182b7b730487dc4e329ba8f853e008f107
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.plugins.design.batch;
21
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;
46
47 @SupportedEnvironment("maven")
48 public class MavenDependenciesSensor implements Sensor {
49
50   private ArtifactRepository localRepository;
51   private ArtifactFactory artifactFactory;
52   private ArtifactMetadataSource artifactMetadataSource;
53   private ArtifactCollector artifactCollector;
54   private DependencyTreeBuilder treeBuilder;
55   private SonarIndex index;
56
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;
63     this.index = index;
64     this.treeBuilder = treeBuilder;
65   }
66
67   public boolean shouldExecuteOnProject(Project project) {
68     return true;
69   }
70
71   public void analyse(final Project project, final SensorContext context) {
72     try {
73       DependencyNode root = treeBuilder.buildDependencyTree(project.getPom(), localRepository, artifactFactory, artifactMetadataSource, null, artifactCollector);
74
75       DependencyNodeVisitor visitor = new BuildingDependencyNodeVisitor(new DependencyNodeVisitor() {
76         public boolean visit(DependencyNode node) {
77           return true;
78         }
79
80         public boolean endVisit(DependencyNode node) {
81           if (node.getParent() != null && node.getParent() != node) {
82             saveDependency(node, context);
83           }
84           return true;
85         }
86       });
87
88       // mode verbose OFF : do not show the same lib many times
89       DependencyNodeFilter filter = StateDependencyNodeFilter.INCLUDED;
90
91       CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
92       DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor(collectingVisitor, filter);
93       root.accept(firstPassVisitor);
94
95       DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
96       visitor = new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
97
98       root.accept(visitor);
99
100     } catch (DependencyTreeBuilderException e) {
101       throw new SonarException("Can not load the graph of dependencies of the project " + project.getKey(), e);
102     }
103   }
104
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);
112   }
113
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);
121     }
122     return result;
123   }
124
125   @Override
126   public String toString() {
127     return "Maven dependencies";
128   }
129 }