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-19 13:49:11 +0200 |
commit | a52fbd8cd565f58464635906c54db7db87b3be77 (patch) | |
tree | 4bdbb503b519d17d179082b9e3bcc5041dc0684c /sonar-maven-plugin | |
parent | 759ac46aaf07593fc0dca30466a440df5cd830c7 (diff) | |
download | sonarqube-a52fbd8cd565f58464635906c54db7db87b3be77.tar.gz sonarqube-a52fbd8cd565f58464635906c54db7db87b3be77.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-maven-plugin')
4 files changed, 129 insertions, 110 deletions
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 @@ <packaging>maven-plugin</packaging> <name>Sonar :: Maven2 Plugin</name> - <properties> - <maven.version>2.0.7</maven.version> - </properties> - <dependencies> <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-dependency-tree</artifactId> - <version>1.2</version> - </dependency> - <dependency> - <groupId>org.codehaus.sonar</groupId> - <artifactId>sonar-batch</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> </dependency> <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-classic</artifactId> + <groupId>org.codehaus.sonar.runner</groupId> + <artifactId>sonar-runner-api</artifactId> + <version>2.3-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> - <version>${maven.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-core</artifactId> - <version>${maven.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> - <version>${maven.version}</version> <scope>provided</scope> </dependency> </dependencies> 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 @@ -69,12 +69,6 @@ public final class SonarMojo extends AbstractMojo { private LifecycleExecutor lifecycleExecutor; /** - * @component - * @required - */ - private PluginManager pluginManager; - - /** * The artifact factory to use. * * @component @@ -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<Entry<Object, Object>> properties = project.getModel().getProperties().entrySet(); + for (Entry<Object, Object> 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 = ','; + +} |