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-maven-plugin | |
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-maven-plugin')
3 files changed, 75 insertions, 29 deletions
diff --git a/sonar-maven-plugin/pom.xml b/sonar-maven-plugin/pom.xml index 6efe191c675..4dadfc59428 100644 --- a/sonar-maven-plugin/pom.xml +++ b/sonar-maven-plugin/pom.xml @@ -18,7 +18,11 @@ <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-dependency-tree</artifactId> - <version>1.2</version> + </dependency> + <dependency> + <groupId>org.codehaus.sonar.runner</groupId> + <artifactId>sonar-runner-api</artifactId> + <version>2.2.2</version> </dependency> <dependency> <groupId>org.codehaus.sonar</groupId> diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java new file mode 100644 index 00000000000..6ec6ea0704a --- /dev/null +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java @@ -0,0 +1,32 @@ +package org.sonar.maven; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.project.MavenProject; +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; + +public class SonarMaven2ProjectBuilder extends ProjectBuilder { + + private MavenSession session; + + public SonarMaven2ProjectBuilder(MavenSession session) { + this.session = session; + } + + @Override + public void build(ProjectBuilderContext context) { + List<MavenProject> 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/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..c5215e28a85 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,8 @@ */ package org.sonar.maven; -import com.google.common.collect.Maps; +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -34,12 +35,14 @@ 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.api.batch.maven.MavenUtils; 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.util.Map.Entry; +import java.util.Set; /** * @goal sonar @@ -134,32 +137,39 @@ 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()); + // Workaround for SONARPLUGINS-2947 + // TODO remove when it will be fixed + runner.setProperty("sonarRunner.userAgent", "Maven"); + runner.setProperty("sonarRunner.userAgentVersion", getMavenVersion()); + Set<Entry<Object, Object>> properties = project.getModel().getProperties().entrySet(); + for (Entry<Object, Object> entry : properties) { + runner.setProperty(ObjectUtils.toString(entry.getKey()), ObjectUtils.toString(entry.getValue())); + } + String encoding = MavenUtils.getSourceEncoding(project); + if (encoding != null) { + runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding); + } + runner.setProperty(ScanProperties.PROJECT_KEY, MavenProjectConverter.getSonarKey(project)) + .setProperty(RunnerProperties.WORK_DIR, MavenProjectConverter.getSonarWorkDir(project).getAbsolutePath()) + .setProperty(ScanProperties.PROJECT_BASEDIR, project.getBasedir().getAbsolutePath()) + .setProperty(ScanProperties.PROJECT_VERSION, StringUtils.defaultString(project.getVersion())) + .setProperty(ScanProperties.PROJECT_NAME, StringUtils.defaultString(project.getName())) + .setProperty(ScanProperties.PROJECT_DESCRIPTION, StringUtils.defaultString(project.getDescription())) + .setProperty(ScanProperties.PROJECT_SOURCE_DIRS, ".") + // Required to share ProjectBuilder extension between SonarMavenProjectBuilder and Sonar classloader + .setUnmaskedPackages("org.sonar.api.batch.bootstrap") + .addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, + dependencyTreeBuilder, projectBuilder, Maven2PluginExecutor.class, new SonarMaven2ProjectBuilder(session)); if (getLog().isDebugEnabled()) { - logging.setVerbose(true); + runner.setProperty("sonar.verbose", "true"); } + runner.execute(); } - private EnvironmentInformation getEnvironmentInformation() { - String mavenVersion = runtimeInformation.getApplicationVersion().toString(); - return new EnvironmentInformation("Maven", mavenVersion); + private String getMavenVersion() { + return runtimeInformation.getApplicationVersion().toString(); } } |