From b863d3fb4ceb9b0a34a8455d7a7263460d4b7807 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 2 Apr 2014 16:55:09 +0200 Subject: [PATCH] SONAR-5190 Make default bootstrapper support Maven --- .../sonar/plugins/maven/MavenBatchPlugin.java | 2 +- .../plugins/maven/MavenProjectBuilder.java | 71 +++++++++++++++++++ .../plugins/maven/MavenBatchPluginTest.java | 2 +- .../batch/scan/ProjectReactorBuilder.java | 26 +------ .../batch/scan/ProjectReactorBuilderTest.java | 8 +-- 5 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectBuilder.java diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java index 05ecc3595c2..64375e5f696 100644 --- a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java @@ -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 index 00000000000..efbef8f43e3 --- /dev/null +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectBuilder.java @@ -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) 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); + } + } + } + +} diff --git a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenBatchPluginTest.java b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenBatchPluginTest.java index 2a7b0739a08..0504cb99f3f 100644 --- a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenBatchPluginTest.java +++ b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenBatchPluginTest.java @@ -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); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java index e1a6fd31e53..17db4e4850a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java @@ -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) 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( diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java index a427a5da689..da0e9626c1c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java @@ -545,7 +545,7 @@ public class ProjectReactorBuilderTest { @Test public void shouldInitRootWorkDir() { - ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(Maps.newHashMap())), null); + ProjectReactorBuilder builder = new ProjectReactorBuilder(new BootstrapSettings(new BootstrapProperties(Maps.newHashMap()))); File baseDir = new File("target/tmp/baseDir"); File workDir = builder.initRootProjectWorkDir(baseDir); @@ -557,7 +557,7 @@ public class ProjectReactorBuilderTest { public void shouldInitWorkDirWithCustomRelativeFolder() { Map props = Maps.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 props = Maps.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(); } -- 2.39.5