started supporting aggregator-style maven build.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@4439 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2007-08-26 16:53:53 +00:00
1 parent bf03fb031c
commit afbe0d2c47
6 files changed
+346 -190

No files matched your search

+57 -75
View File
@@ -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<MavenModule,MavenBuild> {
/**
* Runs Maven and builds the project.
*/
private static final class Builder implements DelegatingCallable<Result,IOException> {
private final BuildListener listener;
private static final class Builder extends MavenBuilder {
private final MavenBuildProxy buildProxy;
private final MavenReporter[] reporters;
private final List<String> 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<String,String> systemProps;
private final List<ExecutedMojo> executedMojos = new ArrayList<ExecutedMojo>();
private long startTime;
public Builder(BuildListener listener,MavenBuildProxy buildProxy,MavenReporter[] reporters, List<String> goals, Map<String,String> 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<MavenModule,MavenBuild> {
/**
* {@link MavenBuildProxy} implementation.
*/
private class ProxyImpl implements MavenBuildProxy, Serializable {
final class ProxyImpl implements MavenBuildProxy, Serializable {
public <V, T extends Throwable> V execute(BuildCallable<V, T> program) throws T, IOException, InterruptedException {
return program.call(MavenBuild.this);
}
@@ -362,27 +364,7 @@ public class MavenBuild extends AbstractBuild<MavenModule,MavenBuild> {
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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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.
@@ -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.
*
* <p>
* As a callable, this function returns the build result.
*
* <p>
* 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<Result,IOException> {
/**
* Goals to be executed in this Maven execution.
*/
private final List<String> 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<String,String> systemProps;
/**
* Where error messages and so on are sent.
*/
protected final BuildListener listener;
protected MavenBuilder(BuildListener listener, List<String> goals, Map<String, String> 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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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;
}
@@ -195,6 +195,11 @@ public final class MavenModule extends AbstractMavenProject<MavenModule,MavenBui
return MavenBuild.class;
}
@Override
protected MavenBuild newBuild() throws IOException {
return super.newBuild();
}
public ModuleName getModuleName() {
return moduleName;
}
@@ -1,5 +1,6 @@
package hudson.maven;
import hudson.AbortException;
import hudson.FilePath.FileCallable;
import hudson.model.AbstractBuild;
import hudson.model.Action;
@@ -8,9 +9,14 @@ import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Result;
import hudson.remoting.VirtualChannel;
import hudson.util.ArgumentListBuilder;
import hudson.util.IOException2;
import hudson.AbortException;
import org.apache.maven.BuildFailureException;
import org.apache.maven.embedder.MavenEmbedderException;
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 org.apache.maven.project.ProjectBuildingException;
@@ -24,8 +30,8 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -271,8 +277,26 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
logger.println("Triggering "+project.getRootModule().getModuleName());
project.getRootModule().scheduleBuild();
} else {
Map<ModuleName,MavenBuild.ProxyImpl> proxies = new HashMap<ModuleName,MavenBuild.ProxyImpl>();
for (MavenModule m : modules.values())
proxies.put(m.getModuleName(),m.newBuild().new ProxyImpl());
// run the complete build here
Map<String,String> 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<MavenModuleSet,Mave
}
}
/**
* Runs Maven and builds the project.
*/
private static final class Builder extends MavenBuilder {
private final Map<ModuleName,? extends MavenBuildProxy> buildProxy;
private final Map<ModuleName,List<MavenReporter>> reporters = new HashMap<ModuleName,List<MavenReporter>>();
public Builder(BuildListener listener,Map<ModuleName,? extends MavenBuildProxy> buildProxy, Collection<MavenModule> modules, List<String> goals, Map<String,String> 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.
@@ -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<ExecutedMojo> executedMojos = new ArrayList<ExecutedMojo>();
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");
}
}
}
@@ -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;
}
}