001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch;
021
022 import org.apache.maven.project.MavenProject;
023 import org.sonar.api.batch.bootstrap.ProjectDefinition;
024 import org.sonar.api.batch.maven.MavenPlugin;
025 import org.sonar.api.batch.maven.MavenPluginHandler;
026 import org.sonar.api.resources.Project;
027 import org.sonar.api.utils.SonarException;
028 import org.sonar.api.utils.TimeProfiler;
029
030 /**
031 * Abstract implementation of {@link MavenPluginExecutor} to reduce duplications in concrete implementations for different Maven versions.
032 */
033 public abstract class AbstractMavenPluginExecutor implements MavenPluginExecutor {
034
035 public final MavenPluginHandler execute(Project project, ProjectDefinition projectDefinition, MavenPluginHandler handler) {
036 for (String goal : handler.getGoals()) {
037 MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
038 execute(project,
039 projectDefinition,
040 getGoal(handler.getGroupId(), handler.getArtifactId(), (plugin != null && plugin.getPlugin() != null ? plugin.getPlugin().getVersion() : null), goal));
041 }
042 return handler;
043 }
044
045 public final void execute(Project project, ProjectDefinition projectDefinition, String goal) {
046 TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
047 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
048 try {
049 concreteExecute(project.getPom(), goal);
050 } catch (Exception e) {
051 throw new SonarException("Unable to execute maven plugin", e);
052 } finally {
053 // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
054 Thread.currentThread().setContextClassLoader(currentClassLoader);
055 profiler.stop();
056 }
057
058 if (project.getPom() != null) {
059 MavenProjectConverter.synchronizeFileSystem(project.getPom(), projectDefinition);
060 }
061 }
062
063 public abstract void concreteExecute(MavenProject pom, String goal) throws Exception;
064
065 static String getGoal(String groupId, String artifactId, String version, String goal) {
066 String defaultVersion = (version == null ? "" : version);
067 return new StringBuilder()
068 .append(groupId).append(":")
069 .append(artifactId).append(":")
070 .append(defaultVersion)
071 .append(":")
072 .append(goal)
073 .toString();
074 }
075
076 }