aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-01-28 23:59:48 +0300
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-01-29 00:42:29 +0300
commitdfbaa4c9d060f5dd32b0c49470d11d8cf51c50f7 (patch)
tree2aae714608edef95a2d46c178ea5d0ae92231d10
parent73e6ee80edae3e5f2a543df88776e1039b425462 (diff)
downloadsonarqube-dfbaa4c9d060f5dd32b0c49470d11d8cf51c50f7.tar.gz
sonarqube-dfbaa4c9d060f5dd32b0c49470d11d8cf51c50f7.zip
SONAR-1800: Fix issue with ClassLoader for Maven 3.x and Java 1.5
* Restore context ClassLoader after execution of Maven plugin. * Add abstract implementation of MavenPluginExecutor to reduce duplications in concrete implementations for different Maven versions.
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java50
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java43
-rw-r--r--sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java44
-rw-r--r--sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java62
-rw-r--r--sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java59
-rw-r--r--sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java56
6 files changed, 131 insertions, 183 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java
new file mode 100644
index 00000000000..7e7bf73b28f
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java
@@ -0,0 +1,50 @@
+package org.sonar.batch;
+
+import org.apache.maven.project.MavenProject;
+import org.sonar.api.batch.maven.MavenPlugin;
+import org.sonar.api.batch.maven.MavenPluginHandler;
+import org.sonar.api.resources.Project;
+import org.sonar.api.utils.SonarException;
+import org.sonar.api.utils.TimeProfiler;
+
+/**
+ * Abstract implementation of {@link MavenPluginExecutor} to reduce duplications in concrete implementations for different Maven versions.
+ */
+public abstract class AbstractMavenPluginExecutor implements MavenPluginExecutor {
+
+ public final MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
+ for (String goal : handler.getGoals()) {
+ MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
+ execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), plugin.getPlugin().getVersion(), goal));
+ }
+ return handler;
+ }
+
+ public final void execute(Project project, String goal) {
+ TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
+ ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ concreteExecute(project.getPom(), goal);
+ } catch (Exception e) {
+ throw new SonarException("Unable to execute maven plugin", e);
+ } finally {
+ // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
+ Thread.currentThread().setContextClassLoader(currentClassLoader);
+ profiler.stop();
+ }
+ }
+
+ public abstract void concreteExecute(MavenProject pom, String goal) throws Exception;
+
+ static String getGoal(String groupId, String artifactId, String version, String goal) {
+ String defaultVersion = (version == null ? "" : version);
+ return new StringBuilder()
+ .append(groupId).append(":")
+ .append(artifactId).append(":")
+ .append(defaultVersion)
+ .append(":")
+ .append(goal)
+ .toString();
+ }
+
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java
new file mode 100644
index 00000000000..e86516f0284
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java
@@ -0,0 +1,43 @@
+package org.sonar.batch;
+
+import org.junit.Test;
+import org.sonar.api.batch.maven.MavenPlugin;
+import org.sonar.api.batch.maven.MavenPluginHandler;
+import org.sonar.api.resources.Project;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class AbstractMavenPluginExecutorTest {
+
+ @Test
+ public void pluginVersionIsOptional() {
+ assertThat(AbstractMavenPluginExecutor.getGoal("group", "artifact", null, "goal"), is("group:artifact::goal"));
+ }
+
+ static class FakeCheckstyleMavenPluginHandler implements MavenPluginHandler {
+ public String getGroupId() {
+ return "org.apache.maven.plugins";
+ }
+
+ public String getArtifactId() {
+ return "maven-checkstyle-plugin";
+ }
+
+ public String getVersion() {
+ return "2.2";
+ }
+
+ public boolean isFixedVersion() {
+ return false;
+ }
+
+ public String[] getGoals() {
+ return new String[] { "checkstyle" };
+ }
+
+ public void configure(Project project, MavenPlugin plugin) {
+ }
+ }
+
+}
diff --git a/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java
index 858f2e52a95..3e0099a4a94 100644
--- a/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java
+++ b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java
@@ -19,19 +19,15 @@
*/
package org.sonar.maven2;
-import org.apache.commons.lang.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.lifecycle.LifecycleExecutor;
-import org.sonar.api.batch.maven.MavenPlugin;
-import org.sonar.api.batch.maven.MavenPluginHandler;
-import org.sonar.api.resources.Project;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.MavenPluginExecutor;
+import org.apache.maven.project.MavenProject;
+import org.sonar.batch.AbstractMavenPluginExecutor;
import java.util.Arrays;
-public class Maven2PluginExecutor implements MavenPluginExecutor {
+public class Maven2PluginExecutor extends AbstractMavenPluginExecutor {
private LifecycleExecutor lifecycleExecutor;
private MavenSession mavenSession;
@@ -41,18 +37,10 @@ public class Maven2PluginExecutor implements MavenPluginExecutor {
this.mavenSession = mavenSession;
}
- public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
- for (String goal : handler.getGoals()) {
- MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
- execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), plugin.getPlugin().getVersion(), goal));
- }
- return handler;
- }
-
- public void execute(Project project, String goal) {
- try {
- ReactorManager reactor = new ReactorManager(Arrays.asList(project.getPom()));
- MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
+ @Override
+ public void concreteExecute(MavenProject pom, String goal) throws Exception {
+ ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
+ MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
mavenSession.getSettings(),
mavenSession.getLocalRepository(),
mavenSession.getEventDispatcher(),
@@ -60,22 +48,8 @@ public class Maven2PluginExecutor implements MavenPluginExecutor {
Arrays.asList(goal),
mavenSession.getExecutionRootDirectory(),
mavenSession.getExecutionProperties(),
- mavenSession.getStartTime()
- );
- lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher());
-
- } catch (Exception e) {
- throw new SonarException("Unable to execute maven plugin", e);
-
- }
+ mavenSession.getStartTime());
+ lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher());
}
- protected static String getGoal(String groupId, String artifactId, String version, String goal) {
- return new StringBuilder()
- .append(groupId).append(":")
- .append(artifactId).append(":")
- .append(StringUtils.defaultString(version, "")).append(":")
- .append(goal)
- .toString();
- }
}
diff --git a/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java b/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java
deleted file mode 100644
index f59f98df3f9..00000000000
--- a/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.maven2;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-import org.sonar.api.batch.maven.MavenPlugin;
-import org.sonar.api.batch.maven.MavenPluginHandler;
-import org.sonar.api.resources.Project;
-
-public class Maven2PluginExecutorTest {
-
- @Test
- public void pluginVersionIsOptional() {
- assertThat(Maven2PluginExecutor.getGoal("group", "artifact", null, "goal"), is("group:artifact::goal"));
- }
-
- static class FakeCheckstyleMavenPluginHandler implements MavenPluginHandler {
-
- public String getGroupId() {
- return "org.apache.maven.plugins";
- }
-
- public String getArtifactId() {
- return "maven-checkstyle-plugin";
- }
-
- public String getVersion() {
- return "2.2";
- }
-
- public boolean isFixedVersion() {
- return false;
- }
-
- public String[] getGoals() {
- return new String[]{"checkstyle"};
- }
-
- public void configure(Project project, MavenPlugin plugin) {
-
- }
- }
-}
diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java
index e39a64415d6..c39c4ebedd2 100644
--- a/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java
+++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/Maven2PluginExecutor.java
@@ -22,15 +22,12 @@ package org.sonar.maven;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.lifecycle.LifecycleExecutor;
-import org.sonar.api.batch.maven.MavenPlugin;
-import org.sonar.api.batch.maven.MavenPluginHandler;
-import org.sonar.api.resources.Project;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.MavenPluginExecutor;
+import org.apache.maven.project.MavenProject;
+import org.sonar.batch.AbstractMavenPluginExecutor;
import java.util.Arrays;
-public class Maven2PluginExecutor implements MavenPluginExecutor {
+public class Maven2PluginExecutor extends AbstractMavenPluginExecutor {
private LifecycleExecutor lifecycleExecutor;
private MavenSession mavenSession;
@@ -40,43 +37,19 @@ public class Maven2PluginExecutor implements MavenPluginExecutor {
this.mavenSession = mavenSession;
}
- public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
- for (String goal : handler.getGoals()) {
- MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
- execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), plugin.getPlugin().getVersion(), goal));
- }
- return handler;
+ @Override
+ public void concreteExecute(MavenProject pom, String goal) throws Exception {
+ ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
+ MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
+ mavenSession.getSettings(),
+ mavenSession.getLocalRepository(),
+ mavenSession.getEventDispatcher(),
+ reactor,
+ Arrays.asList(goal),
+ mavenSession.getExecutionRootDirectory(),
+ mavenSession.getExecutionProperties(),
+ mavenSession.getStartTime());
+ lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher());
}
- public void execute(Project project, String goal) {
- try {
- ReactorManager reactor = new ReactorManager(Arrays.asList(project.getPom()));
- MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
- mavenSession.getSettings(),
- mavenSession.getLocalRepository(),
- mavenSession.getEventDispatcher(),
- reactor,
- Arrays.asList(goal),
- mavenSession.getExecutionRootDirectory(),
- mavenSession.getExecutionProperties(),
- mavenSession.getStartTime()
- );
- lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher());
-
- } catch (Exception e) {
- throw new SonarException("Unable to execute maven plugin", e);
-
- }
- }
-
- protected static String getGoal(String groupId, String artifactId, String version, String goal) {
- String defaultVersion = (version==null ? "" : version);
- return new StringBuilder()
- .append(groupId).append(":")
- .append(artifactId).append(":")
- .append(defaultVersion)
- .append(":")
- .append(goal)
- .toString();
- }
}
diff --git a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java
index b72710b13f9..31fdb5f875b 100644
--- a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java
+++ b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/Maven3PluginExecutor.java
@@ -21,16 +21,12 @@ package org.sonar.maven3;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleExecutor;
-import org.sonar.api.batch.maven.MavenPlugin;
-import org.sonar.api.batch.maven.MavenPluginHandler;
-import org.sonar.api.resources.Project;
-import org.sonar.api.utils.SonarException;
-import org.sonar.api.utils.TimeProfiler;
-import org.sonar.batch.MavenPluginExecutor;
+import org.apache.maven.project.MavenProject;
+import org.sonar.batch.AbstractMavenPluginExecutor;
import java.util.Arrays;
-public class Maven3PluginExecutor implements MavenPluginExecutor {
+public class Maven3PluginExecutor extends AbstractMavenPluginExecutor {
private LifecycleExecutor lifecycleExecutor;
private MavenSession mavenSession;
@@ -40,42 +36,16 @@ public class Maven3PluginExecutor implements MavenPluginExecutor {
this.mavenSession = mavenSession;
}
- public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
- for (String goal : handler.getGoals()) {
- MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
- execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), plugin.getPlugin().getVersion(), goal));
- }
- return handler;
+ @Override
+ public void concreteExecute(MavenProject pom, String goal) {
+ MavenSession projectSession = mavenSession.clone();
+ projectSession.setCurrentProject(pom);
+ projectSession.setProjects(Arrays.asList(pom));
+ projectSession.getRequest().setRecursive(false);
+ projectSession.getRequest().setPom(pom.getFile());
+ projectSession.getRequest().setGoals(Arrays.asList(goal));
+ projectSession.getRequest().setInteractiveMode(false);
+ lifecycleExecutor.execute(projectSession);
}
- public void execute(Project project, String goalOrPhase) {
- TimeProfiler profiler = new TimeProfiler().start("Execute " + goalOrPhase);
- try {
- MavenSession projectSession = mavenSession.clone();
- projectSession.setCurrentProject(project.getPom());
- projectSession.setProjects(Arrays.asList(project.getPom()));
- projectSession.getRequest().setRecursive(false);
- projectSession.getRequest().setPom(project.getPom().getFile());
- projectSession.getRequest().setGoals(Arrays.asList(goalOrPhase));
- projectSession.getRequest().setInteractiveMode(false);
- lifecycleExecutor.execute(projectSession);
-
- } catch (Exception e) {
- throw new SonarException("Unable to execute maven plugin", e);
-
- } finally {
- profiler.stop();
- }
- }
-
- protected static String getGoal(String groupId, String artifactId, String version, String goal) {
- String defaultVersion = (version == null ? "" : version);
- return new StringBuilder()
- .append(groupId).append(":")
- .append(artifactId).append(":")
- .append(defaultVersion)
- .append(":")
- .append(goal)
- .toString();
- }
}