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) {
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);
108 } else if (executeMethod.getParameterTypes().length == 3) {
109 concreteExecuteMaven2(executeMethod, pom, goal);
111 throw new SonarException("Unexpected parameter count on Maven LifecycleExecutor#execute method. Please check your Maven version.");
115 public void concreteExecuteMaven3(MavenProject pom, String goal) {
116 MavenSession projectSession = mavenSession.clone();
117 projectSession.setCurrentProject(pom);
118 projectSession.setProjects(Arrays.asList(pom));
119 projectSession.getRequest().setRecursive(false);
120 projectSession.getRequest().setPom(pom.getFile());
121 projectSession.getRequest().setGoals(Arrays.asList(goal));
122 projectSession.getRequest().setInteractiveMode(false);
123 lifecycleExecutor.execute(projectSession);
124 if (projectSession.getResult().hasExceptions()) {
125 throw new SonarException("Exception during execution of " + goal);
129 public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) {
131 ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
132 MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
133 mavenSession.getSettings(),
134 mavenSession.getLocalRepository(),
135 mavenSession.getEventDispatcher(),
138 mavenSession.getExecutionRootDirectory(),
139 mavenSession.getExecutionProperties(),
140 mavenSession.getStartTime());
141 executeMethod.invoke(lifecycleExecutor, clonedSession, reactor, clonedSession.getEventDispatcher());
142 } catch (Exception e) {
143 throw new SonarException("Unable to execute Maven 2 plugin", e);