diff --git a/core/src/main/java/hudson/maven/MavenBuild.java b/core/src/main/java/hudson/maven/MavenBuild.java index d96c15ade9..161ad456f1 100644 --- a/core/src/main/java/hudson/maven/MavenBuild.java +++ b/core/src/main/java/hudson/maven/MavenBuild.java @@ -2,8 +2,7 @@ package hudson.maven; import hudson.FilePath; import hudson.Util; -import hudson.maven.agent.Main; -import hudson.maven.reporters.SurefireArchiver; +import hudson.maven.agent.AbortException; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; @@ -12,18 +11,19 @@ import hudson.model.Hudson; import hudson.model.Result; import hudson.model.Run; import hudson.remoting.Channel; -import hudson.remoting.DelegatingCallable; import hudson.scm.ChangeLogSet; import hudson.scm.ChangeLogSet.Entry; import hudson.util.ArgumentListBuilder; -import hudson.util.IOException2; -import org.apache.maven.lifecycle.LifecycleExecutorInterceptor; -import org.codehaus.classworlds.NoSuchRealmException; +import org.apache.maven.BuildFailureException; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.execution.ReactorManager; +import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.monitor.event.EventDispatcher; +import org.apache.maven.project.MavenProject; import java.io.File; import java.io.IOException; import java.io.Serializable; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -129,64 +129,66 @@ public class MavenBuild extends AbstractBuild { /** * Runs Maven and builds the project. */ - private static final class Builder implements DelegatingCallable { - private final BuildListener listener; + private static final class Builder extends MavenBuilder { private final MavenBuildProxy buildProxy; private final MavenReporter[] reporters; - private final List goals; + /** - * Hudson-defined system properties. These will be made available to Maven, - * and accessible as if they are specified as -Dkey=value + * Records of what was executed. */ - private final Map systemProps; + private final List executedMojos = new ArrayList(); + + private long startTime; public Builder(BuildListener listener,MavenBuildProxy buildProxy,MavenReporter[] reporters, List goals, Map systemProps) { - this.listener = listener; + super(listener,goals,systemProps); this.buildProxy = buildProxy; this.reporters = reporters; - this.goals = goals; - this.systemProps = systemProps; } - /** - * This code is executed inside the maven jail process. - */ - public Result call() throws IOException { - try { - PluginManagerInterceptor pmi = new PluginManagerInterceptor(buildProxy, reporters, listener); - hudson.maven.agent.PluginManagerInterceptor.setListener(pmi); - LifecycleExecutorInterceptor.setListener(pmi); - - markAsSuccess = false; - - System.getProperties().putAll(systemProps); - - int r = Main.launch(goals.toArray(new String[goals.size()])); - - if(r==0) return Result.SUCCESS; - - if(markAsSuccess) { - listener.getLogger().println("Maven failed with error."); - return Result.SUCCESS; - } - - return Result.FAILURE; - } catch (NoSuchMethodException e) { - throw new IOException2(e); - } catch (IllegalAccessException e) { - throw new IOException2(e); - } catch (NoSuchRealmException e) { - throw new IOException2(e); - } catch (InvocationTargetException e) { - throw new IOException2(e); - } catch (ClassNotFoundException e) { - throw new IOException2(e); - } + @Override + void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + for (MavenReporter r : reporters) + r.preBuild(buildProxy,rm.getTopLevelProject(),listener); } - // since reporters might be from plugins, use the uberjar to resolve them. - public ClassLoader getClassLoader() { - return Hudson.getInstance().getPluginManager().uberClassLoader; + @Override + void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + buildProxy.setExecutedMojos(executedMojos); + for (MavenReporter r : reporters) + r.postBuild(buildProxy,rm.getTopLevelProject(),listener); + } + + @Override + void preExecute(MavenProject project, MojoInfo info) throws IOException, InterruptedException, AbortException { + for (MavenReporter r : reporters) + if(!r.preExecute(buildProxy,project,info,listener)) + throw new AbortException(r+" failed"); + + startTime = System.currentTimeMillis(); + } + + @Override + void postExecute(MavenProject project, MojoInfo info, Exception exception) throws IOException, InterruptedException, AbortException { + executedMojos.add(new ExecutedMojo(info,System.currentTimeMillis()-startTime)); + + for (MavenReporter r : reporters) + if(!r.postExecute(buildProxy,project,info,listener,exception)) + throw new AbortException(r+" failed"); + } + + @Override + void preModule(MavenProject project) throws InterruptedException, IOException, AbortException { + for (MavenReporter r : reporters) + if(!r.enterModule(buildProxy,project,listener)) + throw new AbortException(r+" failed"); + } + + @Override + void postModule(MavenProject project) throws InterruptedException, IOException, AbortException { + for (MavenReporter r : reporters) + if(!r.leaveModule(buildProxy,project,listener)) + throw new AbortException(r+" failed"); } private static final long serialVersionUID = 1L; @@ -195,7 +197,7 @@ public class MavenBuild extends AbstractBuild { /** * {@link MavenBuildProxy} implementation. */ - private class ProxyImpl implements MavenBuildProxy, Serializable { + final class ProxyImpl implements MavenBuildProxy, Serializable { public V execute(BuildCallable program) throws T, IOException, InterruptedException { return program.call(MavenBuild.this); } @@ -362,27 +364,7 @@ public class MavenBuild extends AbstractBuild { private static final int MAX_PROCESS_CACHE = 5; - private static final ProcessCache mavenProcessCache = new ProcessCache(MAX_PROCESS_CACHE); - - /** - * Used by selected {@link MavenReporter}s to notify the maven build agent - * that even though Maven is going to fail, we should report the build as - * success. - * - *

- * This rather ugly hook is necessary to mark builds as unstable, since - * maven considers a test failure to be a build failure, which will otherwise - * mark the build as FAILED. - * - *

- * It's OK for this field to be static, because the JVM where this is actually - * used is in the Maven JVM, so only one build is going on for the whole JVM. - * - *

- * Even though this field is public, please consider this field reserved - * for {@link SurefireArchiver}. Subject to change without notice. - */ - public static boolean markAsSuccess; + protected static final ProcessCache mavenProcessCache = new ProcessCache(MAX_PROCESS_CACHE); /** * Set true to produce debug output. diff --git a/core/src/main/java/hudson/maven/MavenBuilder.java b/core/src/main/java/hudson/maven/MavenBuilder.java new file mode 100644 index 0000000000..31aa4279e0 --- /dev/null +++ b/core/src/main/java/hudson/maven/MavenBuilder.java @@ -0,0 +1,213 @@ +package hudson.maven; + +import hudson.maven.agent.AbortException; +import hudson.maven.agent.Main; +import hudson.maven.agent.PluginManagerInterceptor; +import hudson.maven.agent.PluginManagerListener; +import hudson.maven.reporters.SurefireArchiver; +import hudson.model.BuildListener; +import hudson.model.Hudson; +import hudson.model.Result; +import hudson.remoting.Callable; +import hudson.remoting.DelegatingCallable; +import hudson.util.IOException2; +import org.apache.maven.BuildFailureException; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.execution.ReactorManager; +import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.lifecycle.LifecycleExecutorInterceptor; +import org.apache.maven.lifecycle.LifecycleExecutorListener; +import org.apache.maven.monitor.event.EventDispatcher; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.project.MavenProject; +import org.codehaus.classworlds.NoSuchRealmException; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; +import org.codehaus.plexus.configuration.PlexusConfiguration; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Map; + +/** + * {@link Callable} that invokes Maven CLI (in process) and drives a build. + * + *

+ * As a callable, this function returns the build result. + * + *

+ * This class defines a series of event callbacks, which are invoked during the build. + * This allows subclass to monitor the progress of a build. + * + * @author Kohsuke Kawaguchi + * @since 1.133 + */ +public abstract class MavenBuilder implements DelegatingCallable { + /** + * Goals to be executed in this Maven execution. + */ + private final List goals; + /** + * Hudson-defined system properties. These will be made available to Maven, + * and accessible as if they are specified as -Dkey=value + */ + private final Map systemProps; + /** + * Where error messages and so on are sent. + */ + protected final BuildListener listener; + + protected MavenBuilder(BuildListener listener, List goals, Map systemProps) { + this.listener = listener; + this.goals = goals; + this.systemProps = systemProps; + } + + /** + * Called before the whole build. + */ + abstract void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException; + + /** + * Called after the build has completed fully. + */ + abstract void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException; + + /** + * Called when a build enter another module. + */ + abstract void preModule(MavenProject project) throws InterruptedException, IOException, AbortException; + + /** + * Called when a build leaves a module. + */ + abstract void postModule(MavenProject project) throws InterruptedException, IOException, AbortException; + + /** + * Called before a mojo is executed + */ + abstract void preExecute(MavenProject project, MojoInfo mojoInfo) throws IOException, InterruptedException, AbortException; + + /** + * Called after a mojo has finished executing. + */ + abstract void postExecute(MavenProject project, MojoInfo mojoInfo, Exception exception) throws IOException, InterruptedException, AbortException; + + /** + * This code is executed inside the maven jail process. + */ + public Result call() throws IOException { + try { + Adapter a = new Adapter(this); + PluginManagerInterceptor.setListener(a); + LifecycleExecutorInterceptor.setListener(a); + + markAsSuccess = false; + + System.getProperties().putAll(systemProps); + + int r = Main.launch(goals.toArray(new String[goals.size()])); + + if(r==0) return Result.SUCCESS; + + if(markAsSuccess) { + listener.getLogger().println("Maven failed with error."); + return Result.SUCCESS; + } + + return Result.FAILURE; + } catch (NoSuchMethodException e) { + throw new IOException2(e); + } catch (IllegalAccessException e) { + throw new IOException2(e); + } catch (NoSuchRealmException e) { + throw new IOException2(e); + } catch (InvocationTargetException e) { + throw new IOException2(e); + } catch (ClassNotFoundException e) { + throw new IOException2(e); + } finally { + PluginManagerInterceptor.setListener(null); + LifecycleExecutorInterceptor.setListener(null); + } + } + + // since reporters might be from plugins, use the uberjar to resolve them. + public ClassLoader getClassLoader() { + return Hudson.getInstance().getPluginManager().uberClassLoader; + } + + /** + * Receives {@link PluginManagerListener} and {@link LifecycleExecutorListener} + * and converts them to {@link MavenBuilder}. + */ + private static final class Adapter implements PluginManagerListener, LifecycleExecutorListener { + /** + * Used to detect when to fire {@link MavenReporter#enterModule} + */ + private MavenProject lastModule; + + private final MavenBuilder listener; + + public Adapter(MavenBuilder listener) { + this.listener = listener; + } + + public void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + listener.preBuild(session, rm, dispatcher); + } + + + public void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + fireLeaveModule(); + listener.postBuild(session, rm, dispatcher); + } + + public void preExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException, InterruptedException, AbortException { + if(lastModule!=project) { + // module change + fireLeaveModule(); + fireEnterModule(project); + } + + listener.preExecute(project, new MojoInfo(exec, mergedConfig, eval)); + } + + public void postExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval, Exception exception) throws IOException, InterruptedException, AbortException { + listener.preExecute(project, new MojoInfo(exec, mergedConfig, eval)); + } + + private void fireEnterModule(MavenProject project) throws InterruptedException, IOException, AbortException { + lastModule = project; + listener.preModule(project); + } + + private void fireLeaveModule() throws InterruptedException, IOException, AbortException { + if(lastModule!=null) { + listener.postModule(lastModule); + } + } + } + + /** + * Used by selected {@link MavenReporter}s to notify the maven build agent + * that even though Maven is going to fail, we should report the build as + * success. + * + *

+ * This rather ugly hook is necessary to mark builds as unstable, since + * maven considers a test failure to be a build failure, which will otherwise + * mark the build as FAILED. + * + *

+ * It's OK for this field to be static, because the JVM where this is actually + * used is in the Maven JVM, so only one build is going on for the whole JVM. + * + *

+ * Even though this field is public, please consider this field reserved + * for {@link SurefireArchiver}. Subject to change without notice. + */ + public static boolean markAsSuccess; + + private static final long serialVersionUID = 1L; +} diff --git a/core/src/main/java/hudson/maven/MavenModule.java b/core/src/main/java/hudson/maven/MavenModule.java index c9665ff3e9..f361983d2d 100644 --- a/core/src/main/java/hudson/maven/MavenModule.java +++ b/core/src/main/java/hudson/maven/MavenModule.java @@ -195,6 +195,11 @@ public final class MavenModule extends AbstractMavenProject proxies = new HashMap(); + for (MavenModule m : modules.values()) + proxies.put(m.getModuleName(),m.newBuild().new ProxyImpl()); + // run the complete build here - + Map envVars = getEnvVars(); + + ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get(launcher.getChannel(), listener, + new MavenProcessFactory(project,launcher,envVars)); + + ArgumentListBuilder margs = new ArgumentListBuilder(); + margs.add("-B").add("-f",project.getRootPOM()); + margs.addTokenized(project.getGoals()); + + try { + return process.channel.call(new Builder( + listener,proxies,modules.values(),margs.toList(),envVars)); + } finally { + process.discard(); + } } return null; @@ -299,6 +323,48 @@ public final class MavenModuleSetBuild extends AbstractBuild buildProxy; + private final Map> reporters = new HashMap>(); + + public Builder(BuildListener listener,Map buildProxy, Collection modules, List goals, Map systemProps) { + super(listener,goals,systemProps); + this.buildProxy = buildProxy; + + for (MavenModule m : modules) + reporters.put(m.getModuleName(),m.createReporters()); + } + + void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + // TODO + } + + void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + // TODO + } + + void preModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException { + // TODO + } + + void postModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException { + // TODO + } + + void preExecute(MavenProject project, MojoInfo mojoInfo) throws IOException, InterruptedException, hudson.maven.agent.AbortException { + // TODO + } + + void postExecute(MavenProject project, MojoInfo mojoInfo, Exception exception) throws IOException, InterruptedException, hudson.maven.agent.AbortException { + // TODO + } + + private static final long serialVersionUID = 1L; + } + /** * Executed on the slave to parse POM and extract information into {@link PomInfo}, * which will be then brought back to the master. diff --git a/core/src/main/java/hudson/maven/PluginManagerInterceptor.java b/core/src/main/java/hudson/maven/PluginManagerInterceptor.java deleted file mode 100644 index 89edd4f1fd..0000000000 --- a/core/src/main/java/hudson/maven/PluginManagerInterceptor.java +++ /dev/null @@ -1,111 +0,0 @@ -package hudson.maven; - -import org.apache.maven.plugin.MojoExecution; -import org.apache.maven.project.MavenProject; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.execution.ReactorManager; -import org.apache.maven.monitor.event.EventDispatcher; -import org.apache.maven.BuildFailureException; -import org.apache.maven.lifecycle.LifecycleExecutionException; -import org.apache.maven.lifecycle.LifecycleExecutorListener; -import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; -import org.codehaus.plexus.configuration.PlexusConfiguration; - -import java.io.IOException; -import java.util.List; -import java.util.ArrayList; - -import hudson.model.BuildListener; -import hudson.maven.agent.PluginManagerListener; -import hudson.maven.agent.AbortException; - -/** - * Receives internal maven build events and forward them to the reporters. Used during - * the module build. - * - * @author Kohsuke Kawaguchi - */ -public class PluginManagerInterceptor implements PluginManagerListener, LifecycleExecutorListener { - - private final MavenBuildProxy buildProxy; - private final MavenReporter[] reporters; - private final BuildListener listener; - - /** - * Used to detect when to fire {@link MavenReporter#enterModule} - */ - private MavenProject lastModule; - - /** - * Records of what was executed. - */ - private final List executedMojos = new ArrayList(); - - private long startTime; - - /** - * Called by {@link MavenBuild} to connect this object to the rest of Hudson objects, - * namely {@link MavenBuild}. - */ - public PluginManagerInterceptor(MavenBuildProxy buildProxy, MavenReporter[] reporters, BuildListener listener) { - this.buildProxy = buildProxy; - this.reporters = reporters; - this.listener = listener; - } - - /** - * Called before the whole build. - */ - public void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { - for (MavenReporter r : reporters) - r.preBuild(buildProxy,rm.getTopLevelProject(),listener); - } - - - public void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { - fireLeaveModule(); - buildProxy.setExecutedMojos(executedMojos); - for (MavenReporter r : reporters) - r.postBuild(buildProxy,rm.getTopLevelProject(),listener); - } - - public void preExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException, InterruptedException, AbortException { - if(lastModule!=project) { - // module change - fireLeaveModule(); - fireEnterModule(project); - } - - MojoInfo info = new MojoInfo(exec, mergedConfig, eval); - for (MavenReporter r : reporters) - if(!r.preExecute(buildProxy,project,info,listener)) - throw new AbortException(r+" failed"); - - startTime = System.currentTimeMillis(); - } - - public void postExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval, Exception exception) throws IOException, InterruptedException, AbortException { - MojoInfo info = new MojoInfo(exec, mergedConfig, eval); - - executedMojos.add(new ExecutedMojo(info,System.currentTimeMillis()-startTime)); - - for (MavenReporter r : reporters) - if(!r.postExecute(buildProxy,project,info,listener,exception)) - throw new AbortException(r+" failed"); - } - - private void fireEnterModule(MavenProject project) throws InterruptedException, IOException, AbortException { - lastModule = project; - for (MavenReporter r : reporters) - if(!r.enterModule(buildProxy,project,listener)) - throw new AbortException(r+" failed"); - } - - private void fireLeaveModule() throws InterruptedException, IOException, AbortException { - if(lastModule!=null) { - for (MavenReporter r : reporters) - if(!r.leaveModule(buildProxy,lastModule,listener)) - throw new AbortException(r+" failed"); - } - } -} diff --git a/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java b/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java index 278a5cb13c..fa7b315f48 100644 --- a/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java +++ b/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java @@ -1,6 +1,7 @@ package hudson.maven.reporters; import hudson.maven.MavenBuild; +import hudson.maven.MavenBuilder; import hudson.maven.MavenBuildProxy; import hudson.maven.MavenBuildProxy.BuildCallable; import hudson.maven.MavenModule; @@ -79,7 +80,7 @@ public class SurefireArchiver extends MavenReporter { // if surefire plugin is going to kill maven because of a test failure, // intercept that (or otherwise build will be marked as failure) if(failCount>0 && error instanceof MojoFailureException) { - MavenBuild.markAsSuccess = true; + MavenBuilder.markAsSuccess = true; } }