import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
-import org.sonar.api.batch.SupportedEnvironment;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.batch.SupportedEnvironment;
import org.sonar.api.design.Dependency;
import org.sonar.api.resources.Library;
import org.sonar.api.resources.Project;
private SonarIndex index;
public MavenDependenciesSensor(ArtifactRepository localRepository, ArtifactFactory artifactFactory, ArtifactMetadataSource artifactMetadataSource,
- ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index) {
+ ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index) {
this.localRepository = localRepository;
this.artifactFactory = artifactFactory;
this.artifactMetadataSource = artifactMetadataSource;
public boolean endVisit(DependencyNode node) {
if (node.getParent() != null && node.getParent() != node) {
- saveDependency(node, context);
+ saveDependency(project, node, context);
}
return true;
}
}
}
- protected void saveDependency(DependencyNode node, SensorContext context) {
- Resource from = (node.getParent().getParent() == null) ? index.getProject() : toResource(node.getParent().getArtifact(), context);
- Resource to = toResource(node.getArtifact(), context);
+ protected void saveDependency(final Project project, DependencyNode node, SensorContext context) {
+ Resource from = (node.getParent().getParent() == null) ? index.getProject() : toResource(project, node.getParent().getArtifact(), context);
+ Resource to = toResource(project, node.getArtifact(), context);
Dependency dependency = new Dependency(from, to);
dependency.setUsage(node.getArtifact().getScope());
dependency.setWeight(1);
context.saveDependency(dependency);
}
- protected static Resource toResource(Artifact artifact, SensorContext context) {
- Project project = Project.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId());
- Resource result = context.getResource(project);
+ protected static Resource toResource(final Project project, Artifact artifact, SensorContext context) {
+ Project depWithBranch = Project.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId(), project.getBranch());
+ Resource result = context.getResource(depWithBranch);
if (result == null || !((Project) result).getAnalysisVersion().equals(artifact.getBaseVersion())) {
- Library lib = new Library(project.getKey(), artifact.getBaseVersion());
+ Library lib = Library.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion());
context.saveResource(lib);
result = context.getResource(lib);
}
import org.sonar.api.CoreProperties;
import org.sonar.api.component.Component;
+import javax.annotation.Nullable;
+
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
}
};
- private static final String MAVEN_KEY_FORMAT = "%s:%s";
+ static final String MAVEN_KEY_FORMAT = "%s:%s";
private static final String BRANCH_KEY_FORMAT = "%s:%s";
public static final String SCOPE = Scopes.PROJECT;
}
public static Project createFromMavenIds(String groupId, String artifactId) {
- return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId));
+ return createFromMavenIds(groupId, artifactId, null);
+ }
+
+ public static Project createFromMavenIds(String groupId, String artifactId, @Nullable String branch) {
+ return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId), branch, "");
}
@Override