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 /plugins/sonar-maven-batch-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 'plugins/sonar-maven-batch-plugin')
4 files changed, 243 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar</artifactId> + <version>3.7-SNAPSHOT</version> + </parent> + + <groupId>org.codehaus.sonar.plugins</groupId> + <artifactId>sonar-maven-batch-plugin</artifactId> + <packaging>sonar-plugin</packaging> + <name>Sonar :: Maven Batch Plugin</name> + + <properties> + <maven.api.version>3.0</maven.api.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-batch</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-plugin-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <scope>provided</scope> + </dependency> + <!-- <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <scope>provided</scope> + </dependency> --> + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-packaging-maven-plugin</artifactId> + <configuration> + <pluginName>Maven Batch Plugin</pluginName> + <pluginClass>org.sonar.plugins.maven.MavenBatchPlugin</pluginClass> + </configuration> + </plugin> + </plugins> + </build> +</project> 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<MavenProject> sortedProjects = session.getSortedProjects(); + MavenProject topLevelProject = null; + for (MavenProject project : sortedProjects) { + if (project.isExecutionRoot()) { + topLevelProject = project; + break; + } + } + MavenProjectConverter.configure(context.getProjectReactor().getRoot(), sortedProjects, topLevelProject); + } + +} |