diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-06-17 10:34:10 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-06-17 10:53:35 +0200 |
commit | ceaddeb0db54d29fd67d11f23488f61f85041446 (patch) | |
tree | 4fd16b9f6a79ba2dc84fb61c4733c8d981bfdde0 /sonar-batch | |
parent | c6bdc279d149d83502736ddb0d238fbf5a305998 (diff) | |
download | sonarqube-ceaddeb0db54d29fd67d11f23488f61f85041446.tar.gz sonarqube-ceaddeb0db54d29fd67d11f23488f61f85041446.zip |
SONAR-3979, SONAR-4047 Fix Sonar Maven goal to support Maven 3.1
* now use Sonar Runner embedded to run Sonar
* also updated Maven plugins to make Sonar build pass with Maven 3.1
Diffstat (limited to 'sonar-batch')
5 files changed, 120 insertions, 72 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java index dfd4fcb2bc1..5503d5f03de 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java @@ -55,7 +55,7 @@ public final class Batch { } projectReactor = builder.projectReactor; if (builder.isEnableLoggingConfiguration()) { - logging = LoggingConfiguration.create().setProperties(bootstrapProperties); + logging = LoggingConfiguration.create(builder.environment).setProperties(bootstrapProperties); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java index fc654057d2a..0f87fd7990b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java @@ -19,10 +19,13 @@ */ package org.sonar.batch.bootstrapper; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.core.config.Logback; +import javax.annotation.Nullable; + import java.io.File; import java.util.Map; @@ -43,19 +46,26 @@ public final class LoggingConfiguration { public static final String LEVEL_SQL_RESULTS_VERBOSE = "DEBUG"; public static final String LEVEL_SQL_RESULTS_DEFAULT = "WARN"; - public static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n"; - public static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n"; + @VisibleForTesting + static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n"; + @VisibleForTesting + static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n"; private Map<String, String> substitutionVariables = Maps.newHashMap(); - private LoggingConfiguration() { + private LoggingConfiguration(@Nullable EnvironmentInformation environment) { setVerbose(false); setShowSql(false); - setFormat(FORMAT_DEFAULT); + if (environment != null && "maven".equalsIgnoreCase(environment.getKey())) { + setFormat(FORMAT_MAVEN); + } + else { + setFormat(FORMAT_DEFAULT); + } } - static LoggingConfiguration create() { - return new LoggingConfiguration(); + static LoggingConfiguration create(@Nullable EnvironmentInformation environment) { + return new LoggingConfiguration(environment); } public LoggingConfiguration setProperties(Map<String, String> properties) { @@ -89,7 +99,8 @@ public final class LoggingConfiguration { return addSubstitutionVariable(PROPERTY_SQL_RESULTS_LOGGER_LEVEL, level); } - public LoggingConfiguration setFormat(String format) { + @VisibleForTesting + LoggingConfiguration setFormat(String format) { return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java index 47659b43ace..41f0e8f72e1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java @@ -20,6 +20,8 @@ package org.sonar.batch.scan.maven; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; @@ -50,6 +52,12 @@ public class MavenProjectConverter { } public static 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) { // projects by canonical path to pom.xml Map<String, MavenProject> paths = Maps.newHashMap(); Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap(); @@ -57,7 +65,9 @@ public class MavenProjectConverter { try { for (MavenProject pom : poms) { paths.put(pom.getFile().getCanonicalPath(), pom); - defs.put(pom, convert(pom)); + ProjectDefinition def = pom == root ? rootProjectDefinition : ProjectDefinition.create(); + merge(pom, def); + defs.put(pom, def); } for (Map.Entry<String, MavenProject> entry : paths.entrySet()) { @@ -86,7 +96,6 @@ public class MavenProjectConverter { if (rootProject == null) { throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE); } - return rootProject; } private static MavenProject findMavenProject(final File modulePath, Map<String, MavenProject> paths) throws IOException { @@ -104,27 +113,29 @@ public class MavenProjectConverter { } @VisibleForTesting - static ProjectDefinition convert(MavenProject pom) { - String key = new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString(); - ProjectDefinition definition = ProjectDefinition.create(); + static 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 definition - .setProperties(pom.getModel().getProperties()) - .setKey(key) - .setVersion(pom.getVersion()) - .setName(pom.getName()) - .setDescription(pom.getDescription()) - .addContainerExtension(pom); + .setProperties(pom.getModel().getProperties()) + .setKey(key) + .setVersion(pom.getVersion()) + .setName(pom.getName()) + .setDescription(pom.getDescription()) + .addContainerExtension(pom); guessJavaVersion(pom, definition); guessEncoding(pom, definition); convertMavenLinksToProperties(definition, pom); synchronizeFileSystem(pom, definition); - return definition; + } + + public static String getSonarKey(MavenProject pom) { + return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString(); } private static void guessEncoding(MavenProject pom, ProjectDefinition definition) { - //See http://jira.codehaus.org/browse/SONAR-2151 + // See http://jira.codehaus.org/browse/SONAR-2151 String encoding = MavenUtils.getSourceEncoding(pom); if (encoding != null) { definition.setProperty(CoreProperties.ENCODING_PROPERTY, encoding); @@ -178,27 +189,47 @@ public class MavenProjectConverter { public static void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) { into.setBaseDir(pom.getBasedir()); - File buildDir = resolvePath(pom.getBuild().getDirectory(), pom.getBasedir()); + File buildDir = getBuildDir(pom); if (buildDir != null) { into.setBuildDir(buildDir); - into.setWorkDir(new File(buildDir, "sonar")); + into.setWorkDir(getSonarWorkDir(pom)); } - into.setSourceDirs((String[]) pom.getCompileSourceRoots().toArray(new String[pom.getCompileSourceRoots().size()])); - into.setTestDirs((String[]) pom.getTestCompileSourceRoots().toArray(new String[pom.getTestCompileSourceRoots().size()])); + 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()])); File binaryDir = resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()); if (binaryDir != null) { into.addBinaryDir(binaryDir); } } + public static File getSonarWorkDir(MavenProject pom) { + return new File(getBuildDir(pom), "sonar"); + } + + private static File getBuildDir(MavenProject pom) { + 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>() { + @Override + public boolean apply(String filePath) { + File file = resolvePath(filePath, baseDir); + return file != null && file.exists(); + } + })); + } + public static void synchronizeFileSystem(MavenProject pom, DefaultModuleFileSystem into) { into.resetDirs( - pom.getBasedir(), - resolvePath(pom.getBuild().getDirectory(), pom.getBasedir()), - resolvePaths((List<String>) pom.getCompileSourceRoots(), pom.getBasedir()), - resolvePaths((List<String>) pom.getTestCompileSourceRoots(), pom.getBasedir()), - Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir())) - ); + pom.getBasedir(), + getBuildDir(pom), + resolvePaths((List<String>) pom.getCompileSourceRoots(), pom.getBasedir()), + resolvePaths((List<String>) pom.getTestCompileSourceRoots(), pom.getBasedir()), + Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir())) + ); } static File resolvePath(String path, File basedir) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LoggingConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LoggingConfigurationTest.java index d091aac62a5..1f818ea82e9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LoggingConfigurationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LoggingConfigurationTest.java @@ -20,102 +20,107 @@ package org.sonar.batch.bootstrapper; import com.google.common.collect.Maps; -import org.hamcrest.core.Is; import org.junit.Test; import java.util.Map; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; public class LoggingConfigurationTest { @Test public void testSqlLevel() { - assertThat(LoggingConfiguration.create().setShowSql(true) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_VERBOSE)); + assertThat(LoggingConfiguration.create(null).setShowSql(true) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_VERBOSE); - assertThat(LoggingConfiguration.create().setShowSql(false) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setShowSql(false) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_DEFAULT); - assertThat(LoggingConfiguration.create().setSqlLevel("ERROR") - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is("ERROR")); + assertThat(LoggingConfiguration.create(null).setSqlLevel("ERROR") + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("ERROR"); } @Test public void shouldNotShowSqlByDefault() { - assertThat(LoggingConfiguration.create() - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT)); + assertThat(LoggingConfiguration.create(null) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_DEFAULT); } @Test public void testSetVerbose() { - assertThat(LoggingConfiguration.create().setVerbose(true) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_VERBOSE)); + assertThat(LoggingConfiguration.create(null).setVerbose(true) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE); - assertThat(LoggingConfiguration.create().setVerbose(false) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setVerbose(false) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT); - assertThat(LoggingConfiguration.create().setRootLevel("ERROR") - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is("ERROR")); + assertThat(LoggingConfiguration.create(null).setRootLevel("ERROR") + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("ERROR"); } @Test public void shouldNotBeVerboseByDefault() { - assertThat(LoggingConfiguration.create() - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT)); + assertThat(LoggingConfiguration.create(null) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT); } @Test public void testSetVerboseProperty() { Map<String, String> properties = Maps.newHashMap(); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT); properties.put("sonar.verbose", "true"); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_VERBOSE)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE); properties.put("sonar.verbose", "false"); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT); } @Test public void testSetShowSqlProperty() { Map<String, String> properties = Maps.newHashMap(); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_DEFAULT); properties.put("sonar.showSql", "true"); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_VERBOSE)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_VERBOSE); properties.put("sonar.showSql", "false"); - assertThat(LoggingConfiguration.create().setProperties(properties) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setProperties(properties) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_SQL_DEFAULT); } @Test public void testDefaultFormat() { - assertThat(LoggingConfiguration.create() - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT)); + assertThat(LoggingConfiguration.create(null) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT); + } + + @Test + public void testMavenFormat() { + assertThat(LoggingConfiguration.create(new EnvironmentInformation("maven", "1.0")) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_MAVEN); } @Test public void testSetFormat() { - assertThat(LoggingConfiguration.create().setFormat("%d %level") - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is("%d %level")); + assertThat(LoggingConfiguration.create(null).setFormat("%d %level") + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo("%d %level"); } @Test public void shouldNotSetBlankFormat() { - assertThat(LoggingConfiguration.create().setFormat(null) - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setFormat(null) + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT); - assertThat(LoggingConfiguration.create().setFormat("") - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setFormat("") + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT); - assertThat(LoggingConfiguration.create().setFormat(" ") - .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT)); + assertThat(LoggingConfiguration.create(null).setFormat(" ") + .getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenProjectConverterTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenProjectConverterTest.java index d84808879f3..9150be4031d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenProjectConverterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenProjectConverterTest.java @@ -97,7 +97,8 @@ public class MavenProjectConverterTest { pom.setDescription("just test"); pom.setFile(new File("/foo/pom.xml")); pom.getBuild().setDirectory("target"); - ProjectDefinition project = MavenProjectConverter.convert(pom); + ProjectDefinition project = ProjectDefinition.create(); + MavenProjectConverter.merge(pom, project); Properties properties = project.getProperties(); assertThat(properties.getProperty(CoreProperties.PROJECT_KEY_PROPERTY), is("foo:bar")); |