]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5190 Make default bootstrapper support Maven
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 2 Apr 2014 14:55:09 +0000 (16:55 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 2 Apr 2014 14:55:59 +0000 (16:55 +0200)
plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java
plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectBuilder.java [new file with mode: 0644]
plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenBatchPluginTest.java
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java

index 05ecc3595c2502e288a71eab9d1f71ddfbd12f50..64375e5f69627944a27e47697daaccea31cde87d 100644 (file)
@@ -27,6 +27,6 @@ import java.util.List;
 public final class MavenBatchPlugin extends SonarPlugin {
 
   public List getExtensions() {
-    return ImmutableList.of(MavenProjectBootstrapper.class, DefaultMavenPluginExecutor.class, MavenProjectConverter.class);
+    return ImmutableList.of(MavenProjectBootstrapper.class, DefaultMavenPluginExecutor.class, MavenProjectConverter.class, MavenProjectBuilder.class);
   }
 }
diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectBuilder.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectBuilder.java
new file mode 100644 (file)
index 0000000..efbef8f
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.plugins.maven;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.sonar.api.batch.SupportedEnvironment;
+import org.sonar.api.batch.bootstrap.ProjectBuilder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+
+import java.util.List;
+
+/**
+ * Class that inject MavenProject in each module container
+ */
+@SupportedEnvironment("maven")
+public class MavenProjectBuilder extends ProjectBuilder {
+
+  private final MavenSession mavenSession;
+
+  public MavenProjectBuilder(MavenSession mavenSession) {
+    this.mavenSession = mavenSession;
+  }
+
+  @Override
+  public void build(Context context) {
+    ProjectReactor reactor = context.projectReactor();
+    for (ProjectDefinition moduleDef : reactor.getProjects()) {
+      setMavenProjectIfApplicable(moduleDef);
+    }
+  }
+
+  private void setMavenProjectIfApplicable(ProjectDefinition definition) {
+    if (mavenSession != null) {
+      String moduleKey = definition.getKey();
+      MavenProject foundMavenModule = null;
+      for (MavenProject mavenModule : (List<MavenProject>) mavenSession.getSortedProjects()) {
+        String mavenModuleKey = mavenModule.getGroupId() + ":" + mavenModule.getArtifactId();
+        if (mavenModuleKey.equals(moduleKey)) {
+          foundMavenModule = mavenModule;
+          break;
+        }
+      }
+      if (foundMavenModule == null) {
+        throw new IllegalStateException("Unable to find Maven project in reactor with key " + moduleKey);
+      }
+      if (!definition.getContainerExtensions().contains(foundMavenModule)) {
+        definition.addContainerExtension(foundMavenModule);
+      }
+    }
+  }
+
+}
index 2a7b0739a0875e3e9c9eb60de92c79f4db287388..0504cb99f3f5cef866b41b94452e7a2d7b987b10 100644 (file)
@@ -28,7 +28,7 @@ public class MavenBatchPluginTest {
   @Test
   public void testGetExtensions() {
     MavenBatchPlugin plugin = new MavenBatchPlugin();
-    assertThat(plugin.getExtensions()).hasSize(3);
+    assertThat(plugin.getExtensions()).hasSize(4);
   }
 
 }
index e1a6fd31e538de991ac2ce276fe356f56493c172..17db4e4850af592e6bdb7c3e52aab4742ee8be3a 100644 (file)
@@ -27,8 +27,6 @@ import org.apache.commons.io.filefilter.FileFileFilter;
 import org.apache.commons.io.filefilter.IOFileFilter;
 import org.apache.commons.io.filefilter.WildcardFileFilter;
 import org.apache.commons.lang.StringUtils;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.project.MavenProject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.CoreProperties;
@@ -38,7 +36,6 @@ import org.sonar.batch.bootstrap.BootstrapSettings;
 import org.sonar.core.component.ComponentKeys;
 
 import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
 
 import java.io.File;
 import java.io.FileFilter;
@@ -103,11 +100,9 @@ public class ProjectReactorBuilder {
 
   private BootstrapSettings settings;
   private File rootProjectWorkDir;
-  private MavenSession mavenSession;
 
-  ProjectReactorBuilder(BootstrapSettings settings, @Nullable MavenSession mavenSession) {
+  public ProjectReactorBuilder(BootstrapSettings settings) {
     this.settings = settings;
-    this.mavenSession = mavenSession;
   }
 
   public ProjectReactor execute() {
@@ -141,28 +136,9 @@ public class ProjectReactorBuilder {
       .setBaseDir(baseDir)
       .setWorkDir(workDir)
       .setBuildDir(initModuleBuildDir(baseDir, properties));
-    setMavenProjectIfApplicable(definition);
     return definition;
   }
 
-  private void setMavenProjectIfApplicable(ProjectDefinition definition) {
-    if (mavenSession != null) {
-      String moduleKey = definition.getKey();
-      MavenProject foundMavenModule = null;
-      for (MavenProject mavenModule : (List<MavenProject>) mavenSession.getSortedProjects()) {
-        String mavenModuleKey = mavenModule.getGroupId() + ":" + mavenModule.getArtifactId();
-        if (mavenModuleKey.equals(moduleKey)) {
-          foundMavenModule = mavenModule;
-          break;
-        }
-      }
-      if (foundMavenModule == null) {
-        throw new IllegalStateException("Unable to find Maven project in reactor with key " + moduleKey);
-      }
-      definition.addContainerExtension(foundMavenModule);
-    }
-  }
-
   private void checkProjectKeyValid(String projectKey) {
     if (!ComponentKeys.isValidModuleKey(projectKey)) {
       throw new IllegalStateException(String.format(
index a427a5da6894e0b64b561db034f2f0c9e8853184..da0e9626c1c77e9382802c99829c3c9d1453fc5d 100644 (file)
@@ -545,7 +545,7 @@ public class ProjectReactorBuilderTest {
 
   @Test
   public void shouldInitRootWorkDir() {
-    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(Maps.<String, String>newHashMap())), null);
+    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(Maps.<String, String>newHashMap())));
     File baseDir = new File("target/tmp/baseDir");
 
     File workDir = builder.initRootProjectWorkDir(baseDir);
@@ -557,7 +557,7 @@ public class ProjectReactorBuilderTest {
   public void shouldInitWorkDirWithCustomRelativeFolder() {
     Map<String, String> props = Maps.<String, String>newHashMap();
     props.put("sonar.working.directory", ".foo");
-    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(props)), null);
+    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(props)));
     File baseDir = new File("target/tmp/baseDir");
 
     File workDir = builder.initRootProjectWorkDir(baseDir);
@@ -569,7 +569,7 @@ public class ProjectReactorBuilderTest {
   public void shouldInitRootWorkDirWithCustomAbsoluteFolder() {
     Map<String, String> props = Maps.<String, String>newHashMap();
     props.put("sonar.working.directory", new File("src").getAbsolutePath());
-    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(props)), null);
+    ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(props)));
     File baseDir = new File("target/tmp/baseDir");
 
     File workDir = builder.initRootProjectWorkDir(baseDir);
@@ -634,7 +634,7 @@ public class ProjectReactorBuilderTest {
     }
     props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
     BootstrapProperties bootstrapProps = new BootstrapProperties(props);
-    ProjectReactor projectReactor = new ProjectReactorBuilder(new BootstrapSettings(bootstrapProps), null).execute();
+    ProjectReactor projectReactor = new ProjectReactorBuilder(new BootstrapSettings(bootstrapProps)).execute();
     return projectReactor.getRoot();
   }