From: Julien HENRY Date: Mon, 17 Jun 2013 08:34:10 +0000 (+0200) Subject: SONAR-3979, SONAR-4047 Fix Sonar Maven goal to support Maven 3.1 X-Git-Tag: 3.7~442 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a52fbd8cd565f58464635906c54db7db87b3be77;p=sonarqube.git 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 --- diff --git a/plugins/sonar-maven-batch-plugin/pom.xml b/plugins/sonar-maven-batch-plugin/pom.xml new file mode 100644 index 00000000000..8fc9fe17e96 --- /dev/null +++ b/plugins/sonar-maven-batch-plugin/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.codehaus.sonar + sonar + 3.7-SNAPSHOT + + + org.codehaus.sonar.plugins + sonar-maven-batch-plugin + sonar-plugin + Sonar :: Maven Batch Plugin + + + 3.0 + + + + + org.codehaus.sonar + sonar-batch + ${project.version} + provided + + + org.codehaus.sonar + sonar-plugin-api + provided + + + org.apache.maven + maven-plugin-api + provided + + + org.apache.maven + maven-core + provided + + + + + + + + + org.codehaus.sonar + sonar-packaging-maven-plugin + + Maven Batch Plugin + org.sonar.plugins.maven.MavenBatchPlugin + + + + + 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 new file mode 100644 index 00000000000..d8a44dd84a1 --- /dev/null +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenBatchPlugin.java @@ -0,0 +1,33 @@ +/* + * 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 com.google.common.collect.ImmutableList; +import org.sonar.api.SonarPlugin; + +import java.util.List; + +public final class MavenBatchPlugin extends SonarPlugin { + + public List getExtensions() { + return ImmutableList.builder().add(SonarMavenProjectBuilder.class, MavenPluginExecutor.class) + .build(); + } +} diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenPluginExecutor.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenPluginExecutor.java new file mode 100644 index 00000000000..d11003cc9af --- /dev/null +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenPluginExecutor.java @@ -0,0 +1,96 @@ +/* + * 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.execution.ReactorManager; +import org.apache.maven.lifecycle.LifecycleExecutor; +import org.apache.maven.project.MavenProject; +import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.task.TaskExtension; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.scan.maven.AbstractMavenPluginExecutor; + +import java.lang.reflect.Method; +import java.util.Arrays; + +@SupportedEnvironment("maven") +public class MavenPluginExecutor extends AbstractMavenPluginExecutor implements TaskExtension { + + private LifecycleExecutor lifecycleExecutor; + private MavenSession mavenSession; + + public MavenPluginExecutor(LifecycleExecutor le, MavenSession mavenSession) { + this.lifecycleExecutor = le; + this.mavenSession = mavenSession; + } + + @Override + public void concreteExecute(MavenProject pom, String goal) throws Exception { + Method executeMethod = null; + for (Method m : lifecycleExecutor.getClass().getMethods()) { + if (m.getName().equals("execute")) { + executeMethod = m; + break; + } + } + if (executeMethod == null) { + throw new SonarException("Unable to find execute method on Maven LifecycleExecutor. Please check your Maven version."); + } + if (executeMethod.getParameterTypes().length == 1) { + concreteExecuteMaven3(pom, goal); + } + else if (executeMethod.getParameterTypes().length == 3) { + concreteExecuteMaven2(executeMethod, pom, goal); + } + else { + throw new SonarException("Unexpected parameter count on Maven LifecycleExecutor#execute method. Please check your Maven version."); + } + } + + public void concreteExecuteMaven3(MavenProject pom, String goal) { + MavenSession projectSession = mavenSession.clone(); + projectSession.setCurrentProject(pom); + projectSession.setProjects(Arrays.asList(pom)); + projectSession.getRequest().setRecursive(false); + projectSession.getRequest().setPom(pom.getFile()); + projectSession.getRequest().setGoals(Arrays.asList(goal)); + projectSession.getRequest().setInteractiveMode(false); + lifecycleExecutor.execute(projectSession); + if (projectSession.getResult().hasExceptions()) { + throw new SonarException("Exception during execution of " + goal); + } + } + + public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) throws Exception { + ReactorManager reactor = new ReactorManager(Arrays.asList(pom)); + MavenSession clonedSession = new MavenSession(mavenSession.getContainer(), + mavenSession.getSettings(), + mavenSession.getLocalRepository(), + mavenSession.getEventDispatcher(), + reactor, + Arrays.asList(goal), + mavenSession.getExecutionRootDirectory(), + mavenSession.getExecutionProperties(), + mavenSession.getStartTime()); + executeMethod.invoke(lifecycleExecutor, clonedSession, reactor, clonedSession.getEventDispatcher()); + } + +} diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/SonarMavenProjectBuilder.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/SonarMavenProjectBuilder.java new file mode 100644 index 00000000000..d8b66f64743 --- /dev/null +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/SonarMavenProjectBuilder.java @@ -0,0 +1,53 @@ +/* + * 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.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.batch.bootstrap.ProjectBuilder; +import org.sonar.api.batch.bootstrap.ProjectBuilderContext; +import org.sonar.batch.scan.maven.MavenProjectConverter; + +import java.util.List; + +@SupportedEnvironment("maven") +public class SonarMavenProjectBuilder extends ProjectBuilder { + + private MavenSession session; + + public SonarMavenProjectBuilder(MavenSession session) { + this.session = session; + } + + @Override + public void build(ProjectBuilderContext context) { + List sortedProjects = session.getSortedProjects(); + MavenProject topLevelProject = null; + for (MavenProject project : sortedProjects) { + if (project.isExecutionRoot()) { + topLevelProject = project; + break; + } + } + MavenProjectConverter.configure(context.getProjectReactor().getRoot(), sortedProjects, topLevelProject); + } + +} diff --git a/pom.xml b/pom.xml index 101d01f31c8..5ab55150699 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ plugins/sonar-design-plugin plugins/sonar-l10n-en-plugin plugins/sonar-email-notifications-plugin + plugins/sonar-maven-batch-plugin @@ -77,6 +78,7 @@ 7.6.11.v20130520 UTF-8 2.2.1 + 2.2.0 1.6 ${maven.build.timestamp} yyyy-MM-dd'T'HH:mm:ssZ @@ -153,13 +155,12 @@ org.apache.maven.plugins maven-compiler-plugin - 3.0 + 3.1 - org.apache.maven.plugins maven-dependency-plugin - 2.5.1 + 2.7 org.apache.maven.plugins @@ -174,7 +175,7 @@ org.apache.maven.plugins maven-failsafe-plugin - 2.12 + 2.15 org.apache.maven.plugins @@ -219,7 +220,7 @@ org.apache.maven.plugins maven-plugin-plugin - 2.9 + 3.2 org.apache.maven.plugins @@ -250,12 +251,12 @@ org.apache.maven.plugins maven-surefire-plugin - 2.12.4 + 2.14.1 org.apache.maven.plugins maven-war-plugin - 2.1.1 + 2.3 @@ -266,7 +267,7 @@ org.codehaus.sonar sonar-packaging-maven-plugin - 1.6 + 1.7 @@ -902,7 +903,7 @@ org.apache.maven maven-core - 2.0.7 + ${maven.api.version} @@ -914,17 +915,17 @@ org.apache.maven maven-plugin-api - 2.0.7 + ${maven.api.version} org.apache.maven maven-artifact - 2.0.7 + ${maven.api.version} org.apache.maven maven-project - 2.0.7 + ${maven.api.version} @@ -936,7 +937,7 @@ org.apache.maven.shared maven-dependency-tree - 1.2 + 2.1 @@ -948,7 +949,7 @@ org.apache.maven.shared maven-common-artifact-filters - 1.2 + 1.4 org.jruby diff --git a/sonar-application/pom.xml b/sonar-application/pom.xml index 81e7e9a90a3..7f271b3e6be 100644 --- a/sonar-application/pom.xml +++ b/sonar-application/pom.xml @@ -146,6 +146,13 @@ sonar-java-plugin runtime + + org.codehaus.sonar.plugins + sonar-maven-batch-plugin + ${project.version} + runtime + + org.sonatype.jsw-binaries jsw-binaries 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 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 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 poms, MavenProject root) { + ProjectDefinition def = ProjectDefinition.create(); + configure(def, poms, root); + return def; + } + + public static void configure(ProjectDefinition rootProjectDefinition, List poms, MavenProject root) { // projects by canonical path to pom.xml Map paths = Maps.newHashMap(); Map 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 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 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 filteredCompileSourceRoots = filterExisting(pom.getCompileSourceRoots(), pom.getBasedir()); + List 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 filterExisting(List filePaths, final File baseDir) { + return Lists.newArrayList(Collections2.filter(filePaths, new Predicate() { + @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) pom.getCompileSourceRoots(), pom.getBasedir()), - resolvePaths((List) pom.getTestCompileSourceRoots(), pom.getBasedir()), - Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir())) - ); + pom.getBasedir(), + getBuildDir(pom), + resolvePaths((List) pom.getCompileSourceRoots(), pom.getBasedir()), + resolvePaths((List) 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 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 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 bdb65b90861..f4fc136cab9 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 @@ -105,7 +105,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")); diff --git a/sonar-maven-plugin/pom.xml b/sonar-maven-plugin/pom.xml index 6efe191c675..e6136ee79d8 100644 --- a/sonar-maven-plugin/pom.xml +++ b/sonar-maven-plugin/pom.xml @@ -10,45 +10,29 @@ maven-plugin Sonar :: Maven2 Plugin - - 2.0.7 - - org.apache.maven.shared maven-dependency-tree - 1.2 - - - org.codehaus.sonar - sonar-batch - ${project.version} - - - org.slf4j - slf4j-api - ch.qos.logback - logback-classic + org.codehaus.sonar.runner + sonar-runner-api + 2.3-SNAPSHOT org.apache.maven maven-plugin-api - ${maven.version} provided org.apache.maven maven-core - ${maven.version} provided org.apache.maven maven-project - ${maven.version} provided diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java deleted file mode 100644 index c9f0690aa23..00000000000 --- a/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.maven; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.execution.ReactorManager; -import org.apache.maven.lifecycle.LifecycleExecutor; -import org.apache.maven.project.MavenProject; -import org.sonar.batch.scan.maven.AbstractMavenPluginExecutor; - -import java.util.Arrays; - -public class Maven2PluginExecutor extends AbstractMavenPluginExecutor { - - private LifecycleExecutor lifecycleExecutor; - private MavenSession mavenSession; - - public Maven2PluginExecutor(LifecycleExecutor le, MavenSession mavenSession) { - this.lifecycleExecutor = le; - this.mavenSession = mavenSession; - } - - @Override - public void concreteExecute(MavenProject pom, String goal) throws Exception { - ReactorManager reactor = new ReactorManager(Arrays.asList(pom)); - MavenSession clonedSession = new MavenSession(mavenSession.getContainer(), - mavenSession.getSettings(), - mavenSession.getLocalRepository(), - mavenSession.getEventDispatcher(), - reactor, - Arrays.asList(goal), - mavenSession.getExecutionRootDirectory(), - mavenSession.getExecutionProperties(), - mavenSession.getStartTime()); - lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher()); - } - -} diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java index ffce333b4c3..0a184927385 100644 --- a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java @@ -19,7 +19,6 @@ */ package org.sonar.maven; -import com.google.common.collect.Maps; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -30,16 +29,17 @@ import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.PluginManager; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.batch.scan.maven.MavenProjectConverter; -import org.sonar.batch.bootstrapper.Batch; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.batch.bootstrapper.LoggingConfiguration; +import org.sonar.runner.api.EmbeddedRunner; +import org.sonar.runner.api.RunnerProperties; +import org.sonar.runner.api.ScanProperties; + +import java.io.File; +import java.io.IOException; +import java.util.Map.Entry; +import java.util.Set; /** * @goal sonar @@ -68,12 +68,6 @@ public final class SonarMojo extends AbstractMojo { */ private LifecycleExecutor lifecycleExecutor; - /** - * @component - * @required - */ - private PluginManager pluginManager; - /** * The artifact factory to use. * @@ -134,32 +128,82 @@ public final class SonarMojo extends AbstractMojo { private RuntimeInformation runtimeInformation; public void execute() throws MojoExecutionException, MojoFailureException { - ProjectDefinition def = MavenProjectConverter.convert(session.getSortedProjects(), project); - ProjectReactor reactor = new ProjectReactor(def); - - Batch batch = Batch.builder() - .setEnvironment(getEnvironmentInformation()) - .setProjectReactor(reactor) - .addComponents( - session, getLog(), lifecycleExecutor, pluginManager, artifactFactory, - localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder, - projectBuilder, Maven2PluginExecutor.class) - .build(); - - configureLogging(batch.getLoggingConfiguration()); - batch.execute(); - } - private void configureLogging(LoggingConfiguration logging) { - logging.setProperties(Maps.fromProperties(session.getExecutionProperties())); - logging.setFormat(LoggingConfiguration.FORMAT_MAVEN); + EmbeddedRunner runner = EmbeddedRunner.create() + .setApp("Maven", getMavenVersion()); + Set> properties = project.getModel().getProperties().entrySet(); + for (Entry entry : properties) { + runner.setProperty(toString(entry.getKey()), toString(entry.getValue())); + } + String encoding = getSourceEncoding(project); + if (encoding != null) { + runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding); + } + runner + .setProperty(ScanProperties.PROJECT_KEY, getSonarKey(project)) + .setProperty(RunnerProperties.WORK_DIR, getSonarWorkDir(project).getAbsolutePath()) + .setProperty(ScanProperties.PROJECT_BASEDIR, project.getBasedir().getAbsolutePath()) + .setProperty(ScanProperties.PROJECT_VERSION, toString(project.getVersion())) + .setProperty(ScanProperties.PROJECT_NAME, toString(project.getName())) + .setProperty(ScanProperties.PROJECT_DESCRIPTION, toString(project.getDescription())) + .setProperty(ScanProperties.PROJECT_SOURCE_DIRS, "."); + // Exclude log implementation to not conflict with Maven 3.1 logging impl + runner.mask("org.slf4j.LoggerFactory") + // Include slf4j Logger that is exposed by some Sonar components + .unmask("org.slf4j.Logger") + .unmask("org.slf4j.ILoggerFactory") + // Exclude other slf4j classes + // .unmask("org.slf4j.impl.") + .mask("org.slf4j.") + // Exclude logback + .mask("ch.qos.logback."); + runner.mask("org.sonar."); + // Include everything else + runner.unmask("") + .addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, + dependencyTreeBuilder, projectBuilder); if (getLog().isDebugEnabled()) { - logging.setVerbose(true); + runner.setProperty("sonar.verbose", "true"); } + runner.execute(); + } + + private String getMavenVersion() { + return runtimeInformation.getApplicationVersion().toString(); } - private EnvironmentInformation getEnvironmentInformation() { - String mavenVersion = runtimeInformation.getApplicationVersion().toString(); - return new EnvironmentInformation("Maven", mavenVersion); + public static String toString(Object obj) { + return obj == null ? "" : obj.toString(); + } + + public static String getSourceEncoding(MavenProject pom) { + return pom.getProperties().getProperty("project.build.sourceEncoding"); + } + + public static String getSonarKey(MavenProject pom) { + return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString(); + } + + 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()); + } + + static File resolvePath(String path, File basedir) { + if (path != null) { + File file = new File(path); + if (!file.isAbsolute()) { + try { + file = new File(basedir, path).getCanonicalFile(); + } catch (IOException e) { + throw new RuntimeException("Unable to resolve path '" + path + "'", e); + } + } + return file; + } + return null; } } diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarProperties.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarProperties.java new file mode 100644 index 00000000000..530c601f9c2 --- /dev/null +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarProperties.java @@ -0,0 +1,46 @@ +/* + * Sonar Eclipse + * Copyright (C) 2010-2013 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.maven; + +public interface SonarProperties { + + String PROJECT_BRANCH_PROPERTY = "sonar.branch"; + String PROJECT_VERSION_PROPERTY = "sonar.projectVersion"; + String PROJECT_KEY_PROPERTY = "sonar.projectKey"; + String PROJECT_NAME_PROPERTY = "sonar.projectName"; + String PROJECT_DESCRIPTION_PROPERTY = "sonar.projectDescription"; + String PROJECT_LANGUAGE_PROPERTY = "sonar.language"; + String ENCODING_PROPERTY = "sonar.sourceEncoding"; + + String DRY_RUN_PROPERTY = "sonar.dryRun"; + String DRY_RUN_OUTPUT_PROPERTY = "sonar.dryRun.export.path"; + String REPORT_OUTPUT_PROPERTY = "sonar.report.export.path"; + + String SONAR_URL = "sonar.host.url"; + String SONAR_LOGIN = "sonar.login"; + String SONAR_PASSWORD = "sonar.password"; + String PROJECT_BASEDIR = "sonar.projectBaseDir"; + String WORK_DIR = "sonar.working.directory"; + + String VERBOSE_PROPERTY = "sonar.verbose"; + + char SEPARATOR = ','; + +} diff --git a/sonar-maven3-plugin/pom.xml b/sonar-maven3-plugin/pom.xml index 2e5cf98cd49..18dd0a9c87e 100644 --- a/sonar-maven3-plugin/pom.xml +++ b/sonar-maven3-plugin/pom.xml @@ -7,55 +7,13 @@ 3.7-SNAPSHOT sonar-maven3-plugin - maven-plugin + pom Sonar :: Maven3 Plugin - - - 3.0 - - - - - org.apache.maven.shared - maven-dependency-tree - 1.2 - - - org.codehaus.sonar - sonar-batch - ${project.version} - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - - org.apache.maven - maven-plugin-api - ${maven.version} - provided - - - org.apache.maven - maven-core - ${maven.version} - provided - - - org.apache.maven - maven-compat - ${maven.version} - provided - - - org.apache.maven - maven-settings - ${maven.version} - provided - - + + + + sonar-maven-plugin + + + diff --git a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java deleted file mode 100644 index 10dee626d3f..00000000000 --- a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.maven3; - -import java.util.Arrays; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.lifecycle.LifecycleExecutor; -import org.apache.maven.project.MavenProject; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.scan.maven.AbstractMavenPluginExecutor; - -public class Maven3PluginExecutor extends AbstractMavenPluginExecutor { - - private LifecycleExecutor lifecycleExecutor; - private MavenSession mavenSession; - - public Maven3PluginExecutor(LifecycleExecutor le, MavenSession mavenSession) { - this.lifecycleExecutor = le; - this.mavenSession = mavenSession; - } - - @Override - public void concreteExecute(MavenProject pom, String goal) { - MavenSession projectSession = mavenSession.clone(); - projectSession.setCurrentProject(pom); - projectSession.setProjects(Arrays.asList(pom)); - projectSession.getRequest().setRecursive(false); - projectSession.getRequest().setPom(pom.getFile()); - projectSession.getRequest().setGoals(Arrays.asList(goal)); - projectSession.getRequest().setInteractiveMode(false); - lifecycleExecutor.execute(projectSession); - if (projectSession.getResult().hasExceptions()) { - throw new SonarException("Exception during execution of " + goal); - } - } - -} diff --git a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java deleted file mode 100644 index 63ee50dc889..00000000000 --- a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.maven3; - -import com.google.common.collect.Maps; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactCollector; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.execution.RuntimeInformation; -import org.apache.maven.lifecycle.LifecycleExecutor; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.batch.scan.maven.MavenProjectConverter; -import org.sonar.batch.bootstrapper.Batch; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.batch.bootstrapper.LoggingConfiguration; - -/** - * @goal sonar - * @aggregator - * @requiresDependencyResolution test - */ -public final class SonarMojo extends AbstractMojo { - - /** - * @parameter expression="${session}" - * @required - * @readonly - */ - private MavenSession session; - - /** - * @parameter expression="${project}" - * @required - * @readonly - */ - private MavenProject project; - - /** - * @component - * @required - */ - private LifecycleExecutor lifecycleExecutor; - - /** - * The artifact factory to use. - * - * @component - * @required - * @readonly - */ - private ArtifactFactory artifactFactory; - - /** - * The artifact repository to use. - * - * @parameter expression="${localRepository}" - * @required - * @readonly - */ - private ArtifactRepository localRepository; - - /** - * The artifact metadata source to use. - * - * @component - * @required - * @readonly - */ - private ArtifactMetadataSource artifactMetadataSource; - - /** - * The artifact collector to use. - * - * @component - * @required - * @readonly - */ - private ArtifactCollector artifactCollector; - - /** - * The dependency tree builder to use. - * - * @component - * @required - * @readonly - */ - private DependencyTreeBuilder dependencyTreeBuilder; - - /** - * @component - * @required - * @readonly - */ - private MavenProjectBuilder projectBuilder; - - /** - * @component - * @required - * @readonly - */ - private RuntimeInformation runtimeInformation; - - public void execute() throws MojoExecutionException, MojoFailureException { - ProjectDefinition def = MavenProjectConverter.convert(session.getProjects(), project); - ProjectReactor reactor = new ProjectReactor(def); - - Batch batch = Batch.builder() - .setEnvironment(getEnvironmentInformation()) - .setProjectReactor(reactor) - .addComponents( - session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, - dependencyTreeBuilder, projectBuilder, Maven3PluginExecutor.class) - .build(); - - configureLogging(batch.getLoggingConfiguration()); - batch.execute(); - } - - private void configureLogging(LoggingConfiguration logging) { - logging.setProperties(Maps.fromProperties(session.getSystemProperties())); - logging.setFormat(LoggingConfiguration.FORMAT_MAVEN); - if (getLog().isDebugEnabled()) { - logging.setVerbose(true); - } - } - - private EnvironmentInformation getEnvironmentInformation() { - String mavenVersion = runtimeInformation.getApplicationVersion().toString(); - return new EnvironmentInformation("Maven", mavenVersion); - } - -}