From: Julien HENRY Date: Wed, 8 Oct 2014 14:29:21 +0000 (+0200) Subject: SONAR-1587 Fix inter module dependencies when sonar.branch is used X-Git-Tag: 4.5.1-RC1~61 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8833d880ca4ea5bd2c24f3ede76a3831b9b324ab;p=sonarqube.git SONAR-1587 Fix inter module dependencies when sonar.branch is used --- diff --git a/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java b/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java index 088b3d182b7..ce900fd67c8 100644 --- a/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java +++ b/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java @@ -34,10 +34,10 @@ import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeV 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; @@ -55,7 +55,7 @@ public class MavenDependenciesSensor implements Sensor { 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; @@ -79,7 +79,7 @@ public class MavenDependenciesSensor implements Sensor { public boolean endVisit(DependencyNode node) { if (node.getParent() != null && node.getParent() != node) { - saveDependency(node, context); + saveDependency(project, node, context); } return true; } @@ -102,20 +102,20 @@ public class MavenDependenciesSensor implements Sensor { } } - 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); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java index 785937126fc..da5cc35681b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java @@ -87,6 +87,10 @@ public final class Library extends Resource { return false; } + public static Library createFromMavenIds(String groupId, String artifactId, String version) { + return new Library(String.format(Project.MAVEN_KEY_FORMAT, groupId, artifactId), version); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java index 4355838c9e8..64be7f80a54 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java @@ -28,6 +28,8 @@ import org.apache.maven.project.MavenProject; 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; @@ -49,7 +51,7 @@ public class Project extends Resource implements Component { } }; - 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; @@ -481,7 +483,11 @@ public class Project extends Resource implements Component { } 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