public final class MavenBatchPlugin extends SonarPlugin {
public List getExtensions() {
- return ImmutableList.of(SonarMavenProjectBuilder.class, RealMavenPluginExecutor.class);
+ return ImmutableList.of(SonarMavenProjectBuilder.class, RealMavenPluginExecutor.class, MavenProjectConverter.class);
}
}
import org.apache.maven.model.IssueManagement;
import org.apache.maven.model.Scm;
import org.apache.maven.project.MavenProject;
+import org.sonar.api.BatchExtension;
import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.MavenUtils;
import org.sonar.api.utils.SonarException;
import java.util.List;
import java.util.Map;
-public class MavenProjectConverter {
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
+public class MavenProjectConverter implements BatchExtension {
private static final String UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE = "Unable to determine structure of project." +
" Probably you use Maven Advanced Reactor Options, which is not supported by Sonar and should not be used.";
- private MavenProjectConverter() {
- // only static methods
- }
-
- public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
+ public ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
ProjectDefinition def = ProjectDefinition.create();
configure(def, poms, root);
return def;
}
- public static void configure(ProjectDefinition rootProjectDefinition, List<MavenProject> poms, MavenProject root) {
+ public void configure(ProjectDefinition rootProjectDefinition, List<MavenProject> poms, MavenProject root) {
// projects by canonical path to pom.xml
Map<String, MavenProject> paths = Maps.newHashMap();
Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();
public class SonarMavenProjectBuilder extends ProjectBuilder {
private MavenSession session;
+ private MavenProjectConverter mavenProjectConverter;
- public SonarMavenProjectBuilder(MavenSession session) {
+ public SonarMavenProjectBuilder(MavenSession session, MavenProjectConverter mavenProjectConverter) {
this.session = session;
+ this.mavenProjectConverter = mavenProjectConverter;
}
@Override
public void build(ProjectBuilderContext context) {
+ // Don't use session.getTopLevelProject or session.getProjects to keep compatibility with Maven 2
List<MavenProject> sortedProjects = session.getSortedProjects();
MavenProject topLevelProject = null;
for (MavenProject project : sortedProjects) {
break;
}
}
- MavenProjectConverter.configure(context.getProjectReactor().getRoot(), sortedProjects, topLevelProject);
+ mavenProjectConverter.configure(context.getProjectReactor().getRoot(), sortedProjects, topLevelProject);
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class MavenBatchPluginTest {
+
+ @Test
+ public void testGetExtensions() {
+ MavenBatchPlugin plugin = new MavenBatchPlugin();
+ assertThat(plugin.getExtensions()).hasSize(3);
+ }
+
+}
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ private MavenProjectConverter mavenProjectConverter = new MavenProjectConverter();
+
/**
* See SONAR-2681
*/
root.getModules().add("module/pom.xml");
try {
- MavenProjectConverter.convert(Arrays.asList(root), root);
+ mavenProjectConverter.convert(Arrays.asList(root), root);
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage(), containsString("Advanced Reactor Options"));
module.setFile(new File(basedir, "module/pom.xml"));
module.getBuild().setDirectory("target");
module.getBuild().setOutputDirectory("target/classes");
- ProjectDefinition project = MavenProjectConverter.convert(Arrays.asList(root, module), root);
+ ProjectDefinition project = mavenProjectConverter.convert(Arrays.asList(root, module), root);
assertThat(project.getSubProjects().size(), is(1));
}
MavenProject module1 = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/moduleNameShouldEqualArtifactId/module1/pom.xml", false);
MavenProject module2 = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/moduleNameShouldEqualArtifactId/module2/pom.xml", false);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(parent, module1, module2), parent);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(parent, module1, module2), parent);
assertThat(rootDef.getSubProjects().size(), Is.is(2));
assertThat(rootDef.getKey(), Is.is("org.test:parent"));
MavenProject module1 = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/moduleNameDifferentThanArtifactId/path1/pom.xml", false);
MavenProject module2 = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/moduleNameDifferentThanArtifactId/path2/pom.xml", false);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(parent, module1, module2), parent);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(parent, module1, module2), parent);
assertThat(rootDef.getSubProjects().size(), is(2));
assertThat(rootDef.getKey(), is("org.test:parent"));
MavenProject parent = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/mavenProjectFileNameNotEqualsToPomXml/pom.xml", true);
MavenProject module = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/mavenProjectFileNameNotEqualsToPomXml/module/pom_having_different_name.xml", false);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(parent, module), parent);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(parent, module), parent);
assertThat(rootDef.getSubProjects().size(), Is.is(1));
assertThat(rootDef.getKey(), Is.is("org.test:parent"));
File rootDir = TestUtils.getResource("/org/sonar/plugins/maven/MavenProjectConverterTest/singleProjectWithoutModules/");
MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/singleProjectWithoutModules/pom.xml", true);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(pom), pom);
assertThat(rootDef.getKey(), is("org.test:parent"));
assertThat(rootDef.getSubProjects().size(), is(0));
public void shouldConvertLinksToProperties() throws Exception {
MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/projectWithLinks/pom.xml", true);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(pom), pom);
Properties props = rootDef.getProperties();
assertThat(props.getProperty(CoreProperties.LINKS_HOME_PAGE)).isEqualTo("http://home.com");
public void shouldNotConvertLinksToPropertiesIfPropertyAlreadyDefined() throws Exception {
MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml", true);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(pom), pom);
Properties props = rootDef.getProperties();
public void shouldLoadSourceEncoding() throws Exception {
MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/sourceEncoding/pom.xml", true);
- ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+ ProjectDefinition rootDef = mavenProjectConverter.convert(Arrays.asList(pom), pom);
assertThat(rootDef.getProperties().getProperty(CoreProperties.ENCODING_PROPERTY)).isEqualTo("Shift_JIS");
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.sonar.api.batch.bootstrap.ProjectBuilderContext;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class SonarMavenProjectBuilderTest {
+
+ @Test
+ public void testSimpleProject() {
+ MavenSession session = mock(MavenSession.class);
+ MavenProject rootProject = mock(MavenProject.class);
+ when(rootProject.isExecutionRoot()).thenReturn(true);
+ when(session.getSortedProjects()).thenReturn(Arrays.asList(rootProject));
+
+ MavenProjectConverter mavenProjectConverter = mock(MavenProjectConverter.class);
+ SonarMavenProjectBuilder builder = new SonarMavenProjectBuilder(session, mavenProjectConverter);
+
+ ProjectBuilderContext context = mock(ProjectBuilderContext.class);
+ ProjectDefinition root = ProjectDefinition.create();
+ ProjectReactor reactor = new ProjectReactor(root);
+ when(context.getProjectReactor()).thenReturn(reactor);
+
+ builder.build(context);
+
+ ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class);
+ verify(mavenProjectConverter).configure(eq(root), argument.capture(), eq(rootProject));
+ assertThat(argument.getValue()).contains(rootProject);
+ }
+
+ @Test
+ public void testMultimoduleProject() {
+ MavenSession session = mock(MavenSession.class);
+ MavenProject rootProject = mock(MavenProject.class);
+ MavenProject module1 = mock(MavenProject.class);
+ MavenProject module2 = mock(MavenProject.class);
+ when(rootProject.isExecutionRoot()).thenReturn(true);
+ when(module1.isExecutionRoot()).thenReturn(false);
+ when(module2.isExecutionRoot()).thenReturn(false);
+ when(session.getSortedProjects()).thenReturn(Arrays.asList(module1, module2, rootProject));
+
+ MavenProjectConverter mavenProjectConverter = mock(MavenProjectConverter.class);
+ SonarMavenProjectBuilder builder = new SonarMavenProjectBuilder(session, mavenProjectConverter);
+
+ ProjectBuilderContext context = mock(ProjectBuilderContext.class);
+ ProjectDefinition root = ProjectDefinition.create();
+ ProjectReactor reactor = new ProjectReactor(root);
+ when(context.getProjectReactor()).thenReturn(reactor);
+
+ builder.build(context);
+
+ ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class);
+ verify(mavenProjectConverter).configure(eq(root), argument.capture(), eq(rootProject));
+ assertThat(argument.getValue()).contains(module1, module2, rootProject);
+ }
+
+}