001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.maven;
021    
022    import org.apache.maven.execution.MavenSession;
023    import org.apache.maven.execution.ReactorManager;
024    import org.apache.maven.lifecycle.LifecycleExecutor;
025    import org.apache.maven.project.MavenProject;
026    import org.sonar.api.batch.SupportedEnvironment;
027    import org.sonar.api.batch.maven.MavenPlugin;
028    import org.sonar.api.batch.maven.MavenPluginHandler;
029    import org.sonar.api.resources.Project;
030    import org.sonar.api.utils.SonarException;
031    import org.sonar.api.utils.TimeProfiler;
032    import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
033    import org.sonar.batch.scan.maven.MavenPluginExecutor;
034    
035    import javax.annotation.Nullable;
036    import java.lang.reflect.Method;
037    import java.util.Arrays;
038    
039    @SupportedEnvironment("maven")
040    public class DefaultMavenPluginExecutor implements MavenPluginExecutor {
041    
042      private final MavenProjectConverter pomConverter;
043      private LifecycleExecutor lifecycleExecutor;
044      private MavenSession mavenSession;
045    
046      public DefaultMavenPluginExecutor(LifecycleExecutor le, MavenSession mavenSession, MavenProjectConverter pomConverter) {
047        this.lifecycleExecutor = le;
048        this.mavenSession = mavenSession;
049        this.pomConverter = pomConverter;
050      }
051    
052      @Override
053      public final MavenPluginHandler execute(Project project, DefaultModuleFileSystem fs, MavenPluginHandler handler) {
054        for (String goal : handler.getGoals()) {
055          if (goal == null) {
056            throw new IllegalStateException("Maven goal can't be null");
057          }
058          MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
059          execute(project,
060            fs,
061            getGoal(handler.getGroupId(), handler.getArtifactId(), plugin != null && plugin.getPlugin() != null ? plugin.getPlugin().getVersion() : null, goal));
062        }
063        return handler;
064      }
065    
066      @Override
067      public final void execute(Project project, DefaultModuleFileSystem fs, String goal) {
068        if (project.getPom() != null) {
069          TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
070          ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
071          try {
072            concreteExecute(project.getPom(), goal);
073          } catch (Exception e) {
074            throw new SonarException("Unable to execute maven plugin", e);
075          } finally {
076            // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
077            Thread.currentThread().setContextClassLoader(currentClassLoader);
078            profiler.stop();
079          }
080          if (!fs.isInitialized()) {
081            pomConverter.synchronizeFileSystem(project.getPom(), fs);
082          }
083        }
084      }
085    
086      static String getGoal(String groupId, String artifactId, @Nullable String version, String goal) {
087        String defaultVersion = version == null ? "" : version;
088        return new StringBuilder()
089          .append(groupId).append(":")
090          .append(artifactId).append(":")
091          .append(defaultVersion)
092          .append(":")
093          .append(goal)
094          .toString();
095      }
096    
097      public void concreteExecute(MavenProject pom, String goal) {
098        Method executeMethod = null;
099        for (Method m : lifecycleExecutor.getClass().getMethods()) {
100          if ("execute".equals(m.getName())) {
101            executeMethod = m;
102            break;
103          }
104        }
105        if (executeMethod == null) {
106          throw new SonarException("Unable to find execute method on Maven LifecycleExecutor. Please check your Maven version.");
107        }
108        if (executeMethod.getParameterTypes().length == 1) {
109          concreteExecuteMaven3(pom, goal);
110        } else if (executeMethod.getParameterTypes().length == 3) {
111          concreteExecuteMaven2(executeMethod, pom, goal);
112        } else {
113          throw new SonarException("Unexpected parameter count on Maven LifecycleExecutor#execute method. Please check your Maven version.");
114        }
115      }
116    
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);
128        }
129      }
130    
131      public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) {
132        try {
133          ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
134          MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
135            mavenSession.getSettings(),
136            mavenSession.getLocalRepository(),
137            mavenSession.getEventDispatcher(),
138            reactor,
139            Arrays.asList(goal),
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);
146        }
147      }
148    
149    }