diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-03-13 17:55:22 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-03-13 17:55:34 +0100 |
commit | 5ce5f5635db70f2a03d5a51deb279ecbaec2dd4c (patch) | |
tree | 76a303df265e6c4e7cba6b554cdd00362170767c | |
parent | 8aed5e9587b9e46e0c5acd199e231c04e19a003b (diff) | |
download | sonarqube-5ce5f5635db70f2a03d5a51deb279ecbaec2dd4c.tar.gz sonarqube-5ce5f5635db70f2a03d5a51deb279ecbaec2dd4c.zip |
SONAR-926 allow to set the properties sonar.sources and sonar.tests in Maven pom
5 files changed, 64 insertions, 57 deletions
diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/DefaultMavenPluginExecutor.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/DefaultMavenPluginExecutor.java index 0c5dd085108..fc5eef968b4 100644 --- a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/DefaultMavenPluginExecutor.java +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/DefaultMavenPluginExecutor.java @@ -40,12 +40,14 @@ import java.util.Arrays; @SupportedEnvironment("maven") public class DefaultMavenPluginExecutor implements MavenPluginExecutor { + private final MavenProjectConverter pomConverter; private LifecycleExecutor lifecycleExecutor; private MavenSession mavenSession; - public DefaultMavenPluginExecutor(LifecycleExecutor le, MavenSession mavenSession) { + public DefaultMavenPluginExecutor(LifecycleExecutor le, MavenSession mavenSession, MavenProjectConverter pomConverter) { this.lifecycleExecutor = le; this.mavenSession = mavenSession; + this.pomConverter = pomConverter; } @Override @@ -77,7 +79,7 @@ public class DefaultMavenPluginExecutor implements MavenPluginExecutor { profiler.stop(); } if (!fs.isInitialized()) { - MavenProjectConverter.synchronizeFileSystem(project.getPom(), fs); + pomConverter.synchronizeFileSystem(project.getPom(), fs); } } } diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java index e450e42c8ff..546ed8a5714 100644 --- a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java @@ -20,6 +20,7 @@ package org.sonar.plugins.maven; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; @@ -37,10 +38,10 @@ import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; import org.sonar.java.api.JavaUtils; import javax.annotation.Nullable; - import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -113,7 +114,7 @@ public class MavenProjectConverter implements TaskExtension { } @VisibleForTesting - static void merge(MavenProject pom, ProjectDefinition definition) { + void merge(MavenProject pom, ProjectDefinition definition) { String key = getSonarKey(pom); // IMPORTANT NOTE : reference on properties from POM model must not be saved, // instead they should be copied explicitly - see SONAR-2896 @@ -130,7 +131,7 @@ public class MavenProjectConverter implements TaskExtension { synchronizeFileSystem(pom, definition); } - public static String getSonarKey(MavenProject pom) { + private static String getSonarKey(MavenProject pom) { return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString(); } @@ -187,17 +188,15 @@ public class MavenProjectConverter implements TaskExtension { } } - public static void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) { + public void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) { into.setBaseDir(pom.getBasedir()); File buildDir = getBuildDir(pom); if (buildDir != null) { into.setBuildDir(buildDir); into.setWorkDir(getSonarWorkDir(pom)); } - List<String> filteredCompileSourceRoots = filterExisting(pom.getCompileSourceRoots(), pom.getBasedir()); - List<String> filteredTestCompileSourceRoots = filterExisting(pom.getTestCompileSourceRoots(), pom.getBasedir()); - into.setSourceDirs((String[]) filteredCompileSourceRoots.toArray(new String[filteredCompileSourceRoots.size()])); - into.setTestDirs((String[]) filteredTestCompileSourceRoots.toArray(new String[filteredTestCompileSourceRoots.size()])); + into.setSourceDirs(filterExisting(sourceDirs(pom))); + into.setTestDirs(filterExisting(testDirs(pom))); File binaryDir = resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()); if (binaryDir != null) { into.addBinaryDir(binaryDir); @@ -212,22 +211,28 @@ public class MavenProjectConverter implements TaskExtension { return resolvePath(pom.getBuild().getDirectory(), pom.getBasedir()); } - private static List<String> filterExisting(List<String> filePaths, final File baseDir) { - return Lists.newArrayList(Collections2.filter(filePaths, new Predicate<String>() { + private static String[] filterExisting(List<File> files) { + Collection<File> filtered = Collections2.filter(files, new Predicate<File>() { @Override - public boolean apply(String filePath) { - File file = resolvePath(filePath, baseDir); + public boolean apply(File file) { return file != null && file.exists(); } - })); + }); + Collection<String> paths = Collections2.transform(filtered, new Function<File, String>() { + @Override + public String apply(File file) { + return file.getAbsolutePath(); + } + }); + return paths.toArray(new String[paths.size()]); } - public static void synchronizeFileSystem(MavenProject pom, DefaultModuleFileSystem into) { + public void synchronizeFileSystem(MavenProject pom, DefaultModuleFileSystem into) { into.resetDirs( pom.getBasedir(), getBuildDir(pom), - resolvePaths(pom.getCompileSourceRoots(), pom.getBasedir()), - resolvePaths(pom.getTestCompileSourceRoots(), pom.getBasedir()), + sourceDirs(pom), + testDirs(pom), Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()))); } @@ -256,4 +261,26 @@ public class MavenProjectConverter implements TaskExtension { } return result; } + + List<File> sourceDirs(MavenProject pom) { + List<String> paths; + String prop = pom.getProperties().getProperty(ProjectDefinition.SOURCE_DIRS_PROPERTY); + if (prop != null) { + paths = Arrays.asList(StringUtils.split(prop, ",")); + } else { + paths = pom.getCompileSourceRoots(); + } + return resolvePaths(paths, pom.getBasedir()); + } + + List<File> testDirs(MavenProject pom) { + List<String> paths; + String prop = pom.getProperties().getProperty(ProjectDefinition.TEST_DIRS_PROPERTY); + if (prop != null) { + paths = Arrays.asList(StringUtils.split(prop, ",")); + } else { + paths = pom.getTestCompileSourceRoots(); + } + return resolvePaths(paths, pom.getBasedir()); + } } diff --git a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/DefaultMavenPluginExecutorTest.java b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/DefaultMavenPluginExecutorTest.java index c4a4bccd979..fcaa0bf34b5 100644 --- a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/DefaultMavenPluginExecutorTest.java +++ b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/DefaultMavenPluginExecutorTest.java @@ -1,5 +1,3 @@ -package org.sonar.plugins.maven; - /* * SonarQube, open source software quality management tool. * Copyright (C) 2008-2014 SonarSource @@ -19,6 +17,7 @@ package org.sonar.plugins.maven; * 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.project.MavenProject; import org.junit.Test; @@ -49,32 +48,9 @@ public class DefaultMavenPluginExecutorTest { assertThat(DefaultMavenPluginExecutor.getGoal("group", "artifact", "3.54", "goal"), is("group:artifact:3.54:goal")); } - /** - * The maven plugin sometimes changes the project structure (for example mvn build-helper:add-source). These changes - * must be applied to the internal structure. - */ - @Test - public void should_reset_file_system_after_execution() { - DefaultMavenPluginExecutor executor = new DefaultMavenPluginExecutor(null, null) { - @Override - public void concreteExecute(MavenProject pom, String goal) { - pom.addCompileSourceRoot("src/java"); - } - }; - MavenProject pom = new MavenProject(); - pom.setFile(new File("target/AbstractMavenPluginExecutorTest/pom.xml")); - pom.getBuild().setDirectory("target"); - Project foo = new Project("foo"); - foo.setPom(pom); - DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); - executor.execute(foo, fs, new AddSourceMavenPluginHandler()); - - verify(fs).resetDirs(any(File.class), any(File.class), anyList(), anyList(), anyList()); - } - @Test public void should_ignore_non_maven_projects() { - DefaultMavenPluginExecutor executor = new DefaultMavenPluginExecutor(null, null) { + DefaultMavenPluginExecutor executor = new DefaultMavenPluginExecutor(null, null, mock(MavenProjectConverter.class)) { @Override public void concreteExecute(MavenProject pom, String goal) { pom.addCompileSourceRoot("src/java"); @@ -105,7 +81,7 @@ public class DefaultMavenPluginExecutorTest { } public String[] getGoals() { - return new String[] {"fake"}; + return new String[]{"fake"}; } public void configure(Project project, MavenPlugin plugin) { diff --git a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectBootstrapperTest.java b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectBootstrapperTest.java index ad69af43d81..9c7cb4ecde8 100644 --- a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectBootstrapperTest.java +++ b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectBootstrapperTest.java @@ -31,7 +31,9 @@ import java.util.Arrays; import java.util.List; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class MavenProjectBootstrapperTest { diff --git a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectConverterTest.java b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectConverterTest.java index aef1ffa4ef6..6cc799f029c 100644 --- a/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectConverterTest.java +++ b/plugins/sonar-maven-batch-plugin/src/test/java/org/sonar/plugins/maven/MavenProjectConverterTest.java @@ -51,7 +51,7 @@ public class MavenProjectConverterTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - MavenProjectConverter mavenProjectConverter = new MavenProjectConverter(); + MavenProjectConverter converter = new MavenProjectConverter(); /** * See SONAR-2681 @@ -64,7 +64,7 @@ public class MavenProjectConverterTest { root.getModules().add("module/pom.xml"); try { - mavenProjectConverter.configure(Arrays.asList(root), root); + converter.configure(Arrays.asList(root), root); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage(), containsString("Advanced Reactor Options")); @@ -84,7 +84,7 @@ public class MavenProjectConverterTest { module.setFile(new File(basedir, "module/pom.xml")); module.getBuild().setDirectory("target"); module.getBuild().setOutputDirectory("target/classes"); - ProjectDefinition project = mavenProjectConverter.configure(Arrays.asList(root, module), root); + ProjectDefinition project = converter.configure(Arrays.asList(root, module), root); assertThat(project.getSubProjects().size(), is(1)); } @@ -108,7 +108,7 @@ public class MavenProjectConverterTest { pom.setFile(new File("/foo/pom.xml")); pom.getBuild().setDirectory("target"); ProjectDefinition project = ProjectDefinition.create(); - MavenProjectConverter.merge(pom, project); + converter.merge(pom, project); Properties properties = project.getProperties(); assertThat(properties.getProperty(CoreProperties.PROJECT_KEY_PROPERTY), is("foo:bar")); @@ -124,7 +124,7 @@ public class MavenProjectConverterTest { 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.configure(Arrays.asList(parent, module1, module2), parent); + ProjectDefinition rootDef = converter.configure(Arrays.asList(parent, module1, module2), parent); assertThat(rootDef.getSubProjects().size(), Is.is(2)); assertThat(rootDef.getKey(), Is.is("org.test:parent")); @@ -145,7 +145,7 @@ public class MavenProjectConverterTest { 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.configure(Arrays.asList(parent, module1, module2), parent); + ProjectDefinition rootDef = converter.configure(Arrays.asList(parent, module1, module2), parent); assertThat(rootDef.getSubProjects().size(), is(2)); assertThat(rootDef.getKey(), is("org.test:parent")); @@ -165,7 +165,7 @@ public class MavenProjectConverterTest { 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.configure(Arrays.asList(parent, module), parent); + ProjectDefinition rootDef = converter.configure(Arrays.asList(parent, module), parent); assertThat(rootDef.getSubProjects().size(), Is.is(1)); assertThat(rootDef.getKey(), Is.is("org.test:parent")); @@ -184,7 +184,7 @@ public class MavenProjectConverterTest { 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.configure(Arrays.asList(pom), pom); + ProjectDefinition rootDef = converter.configure(Arrays.asList(pom), pom); assertThat(rootDef.getKey(), is("org.test:parent")); assertThat(rootDef.getSubProjects().size(), is(0)); @@ -196,7 +196,7 @@ public class MavenProjectConverterTest { public void shouldConvertLinksToProperties() throws Exception { MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/projectWithLinks/pom.xml", true); - ProjectDefinition rootDef = mavenProjectConverter.configure(Arrays.asList(pom), pom); + ProjectDefinition rootDef = converter.configure(Arrays.asList(pom), pom); Properties props = rootDef.getProperties(); assertThat(props.getProperty(CoreProperties.LINKS_HOME_PAGE)).isEqualTo("http://home.com"); @@ -210,7 +210,7 @@ public class MavenProjectConverterTest { public void shouldNotConvertLinksToPropertiesIfPropertyAlreadyDefined() throws Exception { MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml", true); - ProjectDefinition rootDef = mavenProjectConverter.configure(Arrays.asList(pom), pom); + ProjectDefinition rootDef = converter.configure(Arrays.asList(pom), pom); Properties props = rootDef.getProperties(); @@ -228,7 +228,7 @@ public class MavenProjectConverterTest { public void shouldLoadSourceEncoding() throws Exception { MavenProject pom = loadPom("/org/sonar/plugins/maven/MavenProjectConverterTest/sourceEncoding/pom.xml", true); - ProjectDefinition rootDef = mavenProjectConverter.configure(Arrays.asList(pom), pom); + ProjectDefinition rootDef = converter.configure(Arrays.asList(pom), pom); assertThat(rootDef.getProperties().getProperty(CoreProperties.ENCODING_PROPERTY)).isEqualTo("Shift_JIS"); } |