2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.maven;
22 import org.apache.maven.execution.MavenSession;
23 import org.apache.maven.execution.ReactorManager;
24 import org.apache.maven.lifecycle.LifecycleExecutor;
25 import org.apache.maven.project.MavenProject;
26 import org.sonar.api.batch.SupportedEnvironment;
27 import org.sonar.api.batch.maven.MavenPlugin;
28 import org.sonar.api.batch.maven.MavenPluginHandler;
29 import org.sonar.api.resources.Project;
30 import org.sonar.api.utils.SonarException;
31 import org.sonar.api.utils.TimeProfiler;
32 import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
33 import org.sonar.batch.scan.maven.MavenPluginExecutor;
35 import javax.annotation.Nullable;
37 import java.lang.reflect.Method;
38 import java.util.Arrays;
40 @SupportedEnvironment("maven")
41 public class DefaultMavenPluginExecutor implements MavenPluginExecutor {
43 private LifecycleExecutor lifecycleExecutor;
44 private MavenSession mavenSession;
46 public DefaultMavenPluginExecutor(LifecycleExecutor le, MavenSession mavenSession) {
47 this.lifecycleExecutor = le;
48 this.mavenSession = mavenSession;
52 public final MavenPluginHandler execute(Project project, DefaultModuleFileSystem fs, MavenPluginHandler handler) {
53 for (String goal : handler.getGoals()) {
55 throw new IllegalStateException("Maven goal can't be null");
57 MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
60 getGoal(handler.getGroupId(), handler.getArtifactId(), (plugin != null && plugin.getPlugin() != null ? plugin.getPlugin().getVersion() : null), goal));
66 public final void execute(Project project, DefaultModuleFileSystem fs, String goal) {
67 if (project.getPom() != null) {
68 TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
69 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
71 concreteExecute(project.getPom(), goal);
72 } catch (Exception e) {
73 throw new SonarException("Unable to execute maven plugin", e);
75 // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
76 Thread.currentThread().setContextClassLoader(currentClassLoader);
80 MavenProjectConverter.synchronizeFileSystem(project.getPom(), fs);
84 static String getGoal(String groupId, String artifactId, @Nullable String version, String goal) {
85 String defaultVersion = (version == null ? "" : version);
86 return new StringBuilder()
87 .append(groupId).append(":")
88 .append(artifactId).append(":")
89 .append(defaultVersion)
95 public void concreteExecute(MavenProject pom, String goal) throws SecurityException {
96 Method executeMethod = null;
97 for (Method m : lifecycleExecutor.getClass().getMethods()) {
98 if ("execute".equals(m.getName())) {
103 if (executeMethod == null) {
104 throw new SonarException("Unable to find execute method on Maven LifecycleExecutor. Please check your Maven version.");
106 if (executeMethod.getParameterTypes().length == 1) {
107 concreteExecuteMaven3(pom, goal);
109 else if (executeMethod.getParameterTypes().length == 3) {
110 concreteExecuteMaven2(executeMethod, pom, goal);
113 throw new SonarException("Unexpected parameter count on Maven LifecycleExecutor#execute method. Please check your Maven version.");
117 public void concreteExecuteMaven3(MavenProject pom, String goal) {
118 MavenSession projectSession = mavenSession.clone();
119 projectSession.setCurrentProject(pom);
120 projectSession.setProjects(Arrays.asList(pom));
121 projectSession.getRequest().setRecursive(false);
122 projectSession.getRequest().setPom(pom.getFile());
123 projectSession.getRequest().setGoals(Arrays.asList(goal));
124 projectSession.getRequest().setInteractiveMode(false);
125 lifecycleExecutor.execute(projectSession);
126 if (projectSession.getResult().hasExceptions()) {
127 throw new SonarException("Exception during execution of " + goal);
131 public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) {
133 ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
134 MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
135 mavenSession.getSettings(),
136 mavenSession.getLocalRepository(),
137 mavenSession.getEventDispatcher(),
140 mavenSession.getExecutionRootDirectory(),
141 mavenSession.getExecutionProperties(),
142 mavenSession.getStartTime());
143 executeMethod.invoke(lifecycleExecutor, clonedSession, reactor, clonedSession.getEventDispatcher());
144 } catch (Exception e) {
145 throw new SonarException("Unable to execute Maven 2 plugin", e);