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);
}
}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+ }
+
+}
@Test
public void testGetExtensions() {
MavenBatchPlugin plugin = new MavenBatchPlugin();
- assertThat(plugin.getExtensions()).hasSize(3);
+ assertThat(plugin.getExtensions()).hasSize(4);
}
}
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;
import org.sonar.core.component.ComponentKeys;
import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import java.io.File;
import java.io.FileFilter;
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() {
.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(
@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);
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);
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);
}
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();
}