You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractMavenPluginExecutor.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar 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.
  10. *
  11. * Sonar 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.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.batch;
  21. import org.apache.maven.project.MavenProject;
  22. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  23. import org.sonar.api.batch.maven.MavenPlugin;
  24. import org.sonar.api.batch.maven.MavenPluginHandler;
  25. import org.sonar.api.resources.Project;
  26. import org.sonar.api.utils.SonarException;
  27. import org.sonar.api.utils.TimeProfiler;
  28. /**
  29. * Abstract implementation of {@link MavenPluginExecutor} to reduce duplications in concrete implementations for different Maven versions.
  30. */
  31. public abstract class AbstractMavenPluginExecutor implements MavenPluginExecutor {
  32. public final MavenPluginHandler execute(Project project, ProjectDefinition projectDefinition, MavenPluginHandler handler) {
  33. for (String goal : handler.getGoals()) {
  34. MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
  35. execute(project,
  36. projectDefinition,
  37. getGoal(handler.getGroupId(), handler.getArtifactId(), (plugin != null && plugin.getPlugin() != null ? plugin.getPlugin().getVersion() : null), goal));
  38. }
  39. return handler;
  40. }
  41. public final void execute(Project project, ProjectDefinition projectDefinition, String goal) {
  42. TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
  43. ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  44. try {
  45. concreteExecute(project.getPom(), goal);
  46. } catch (Exception e) {
  47. throw new SonarException("Unable to execute maven plugin", e);
  48. } finally {
  49. // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
  50. Thread.currentThread().setContextClassLoader(currentClassLoader);
  51. profiler.stop();
  52. }
  53. if (project.getPom() != null) {
  54. MavenProjectConverter.synchronizeFileSystem(project.getPom(), projectDefinition);
  55. }
  56. }
  57. public abstract void concreteExecute(MavenProject pom, String goal) throws Exception;
  58. static String getGoal(String groupId, String artifactId, String version, String goal) {
  59. String defaultVersion = (version == null ? "" : version);
  60. return new StringBuilder()
  61. .append(groupId).append(":")
  62. .append(artifactId).append(":")
  63. .append(defaultVersion)
  64. .append(":")
  65. .append(goal)
  66. .toString();
  67. }
  68. }