From e14914e66aee1a671f95e88f835ff1dd8a28289b Mon Sep 17 00:00:00 2001 From: Christoph Kutzinski Date: Mon, 3 Oct 2011 21:03:25 +0200 Subject: [PATCH] - moved MavenModuleSet.Builder to new top-level class Maven2Builder - removed duplicated FilterImpl classes --- .../hudson/maven/AbstractMavenBuilder.java | 37 ++++ .../main/java/hudson/maven/Maven2Builder.java | 180 +++++++++++++++++ .../main/java/hudson/maven/Maven3Builder.java | 41 +--- .../hudson/maven/MavenModuleSetBuild.java | 191 +----------------- 4 files changed, 221 insertions(+), 228 deletions(-) create mode 100644 maven-plugin/src/main/java/hudson/maven/Maven2Builder.java diff --git a/maven-plugin/src/main/java/hudson/maven/AbstractMavenBuilder.java b/maven-plugin/src/main/java/hudson/maven/AbstractMavenBuilder.java index cf12db9350..3b9e524d2c 100644 --- a/maven-plugin/src/main/java/hudson/maven/AbstractMavenBuilder.java +++ b/maven-plugin/src/main/java/hudson/maven/AbstractMavenBuilder.java @@ -23,12 +23,17 @@ */ package hudson.maven; +import hudson.maven.MavenBuildProxy.BuildCallable; +import hudson.maven.MavenBuildProxy.Filter.AsyncInvoker; import hudson.model.BuildListener; import jenkins.model.Jenkins; import hudson.model.Result; +import hudson.remoting.Channel; import hudson.remoting.DelegatingCallable; +import hudson.remoting.Future; import java.io.IOException; +import java.io.Serializable; import java.text.NumberFormat; import java.util.List; import java.util.Map; @@ -39,6 +44,7 @@ import java.util.Map; */ public abstract class AbstractMavenBuilder implements DelegatingCallable { + private static final long serialVersionUID = -2687215937784908860L; /** * Goals to be executed in this Maven execution. */ @@ -53,6 +59,12 @@ public abstract class AbstractMavenBuilder implements DelegatingCallable> futures; + protected AbstractMavenBuilder(BuildListener listener, List goals, Map systemProps) { this.listener = listener; this.goals = goals; @@ -99,4 +111,29 @@ public abstract class AbstractMavenBuilder implements DelegatingCallable implements Serializable { + + private MavenBuildInformation mavenBuildInformation; + + private Channel channel; + + public FilterImpl(MavenBuildProxy2 core, MavenBuildInformation mavenBuildInformation, Channel channel) { + super(core); + this.mavenBuildInformation = mavenBuildInformation; + this.channel = channel; + } + + @Override + public void executeAsync(final BuildCallable program) throws IOException { + futures.add(channel.callAsync(new AsyncInvoker(core,program))); + } + + private static final long serialVersionUID = 1L; + + public MavenBuildInformation getMavenBuildInformation() + { + return mavenBuildInformation; + } + } + } diff --git a/maven-plugin/src/main/java/hudson/maven/Maven2Builder.java b/maven-plugin/src/main/java/hudson/maven/Maven2Builder.java new file mode 100644 index 0000000000..0106619ab3 --- /dev/null +++ b/maven-plugin/src/main/java/hudson/maven/Maven2Builder.java @@ -0,0 +1,180 @@ +package hudson.maven; + +import hudson.Launcher; +import hudson.maven.MavenBuild.ProxyImpl2; +import hudson.model.BuildListener; +import hudson.model.Result; +import hudson.remoting.Channel; +import hudson.util.MaskingClassLoader; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +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; + +/** + * Runs Maven and builds the project. + * + * This is only used for + * {@link MavenModuleSet#isAggregatorStyleBuild() the aggregator style build}. + */ +final class Maven2Builder extends MavenBuilder { + private final Map proxies; + private final Map> reporters = new HashMap>(); + private final Map> executedMojos = new HashMap>(); + private long mojoStartTime; + + private MavenBuildProxy2 lastProxy; + + /** + * Kept so that we can finalize them in the end method. + */ + private final transient Map sourceProxies; + + public Maven2Builder(BuildListener listener,Map proxies, Collection modules, List goals, Map systemProps, MavenBuildInformation mavenBuildInformation) { + super(listener,goals,systemProps); + this.sourceProxies = proxies; + this.proxies = new HashMap(proxies); + for (Entry e : this.proxies.entrySet()) + e.setValue(new FilterImpl(e.getValue(), mavenBuildInformation, Channel.current())); + + for (MavenModule m : modules) + reporters.put(m.getModuleName(),m.createReporters()); + } + + /** + * Invoked after the maven has finished running, and in the master, not in the maven process. + */ + void end(Launcher launcher) throws IOException, InterruptedException { + for (Map.Entry e : sourceProxies.entrySet()) { + ProxyImpl2 p = e.getValue(); + for (MavenReporter r : reporters.get(e.getKey())) { + // we'd love to do this when the module build ends, but doing so requires + // we know how many task segments are in the current build. + r.end(p.owner(),launcher,listener); + p.appendLastLog(); + } + p.close(); + } + } + + @Override + public Result call() throws IOException { + try { + if (MavenModuleSetBuild.debug) { + listener.getLogger().println("Builder extends MavenBuilder in call " + Thread.currentThread().getContextClassLoader()); + } + return super.call(); + } finally { + if(lastProxy!=null) + lastProxy.appendLastLog(); + } + } + + + @Override + void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { + // set all modules which are not actually being build (in incremental builds) to NOT_BUILD + + List projects = rm.getSortedProjects(); + Set buildingProjects = new HashSet(); + for (MavenProject p : projects) { + buildingProjects.add(new ModuleName(p)); + } + + for (Entry e : this.proxies.entrySet()) { + if (! buildingProjects.contains(e.getKey())) { + MavenBuildProxy2 proxy = e.getValue(); + proxy.start(); + proxy.setResult(Result.NOT_BUILT); + proxy.end(); + } + } + } + + 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 { + ModuleName name = new ModuleName(project); + MavenBuildProxy2 proxy = proxies.get(name); + listener.getLogger().flush(); // make sure the data until here are all written + proxy.start(); + for (MavenReporter r : reporters.get(name)) + if(!r.preBuild(proxy,project,listener)) + throw new hudson.maven.agent.AbortException(r+" failed"); + } + + void postModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException { + ModuleName name = new ModuleName(project); + MavenBuildProxy2 proxy = proxies.get(name); + List rs = reporters.get(name); + if(rs==null) { // probe for issue #906 + throw new AssertionError("reporters.get("+name+")==null. reporters="+reporters+" proxies="+proxies); + } + for (MavenReporter r : rs) + if(!r.postBuild(proxy,project,listener)) + throw new hudson.maven.agent.AbortException(r+" failed"); + proxy.setExecutedMojos(executedMojos.get(name)); + listener.getLogger().flush(); // make sure the data until here are all written + proxy.end(); + lastProxy = proxy; + } + + void preExecute(MavenProject project, MojoInfo mojoInfo) throws IOException, InterruptedException, hudson.maven.agent.AbortException { + ModuleName name = new ModuleName(project); + MavenBuildProxy proxy = proxies.get(name); + for (MavenReporter r : reporters.get(name)) + if(!r.preExecute(proxy,project,mojoInfo,listener)) + throw new hudson.maven.agent.AbortException(r+" failed"); + + mojoStartTime = System.currentTimeMillis(); + } + + void postExecute(MavenProject project, MojoInfo mojoInfo, Exception exception) throws IOException, InterruptedException, hudson.maven.agent.AbortException { + ModuleName name = new ModuleName(project); + + List mojoList = executedMojos.get(name); + if(mojoList==null) + executedMojos.put(name,mojoList=new ArrayList()); + mojoList.add(new ExecutedMojo(mojoInfo,System.currentTimeMillis()-mojoStartTime)); + + MavenBuildProxy2 proxy = proxies.get(name); + for (MavenReporter r : reporters.get(name)) + if(!r.postExecute(proxy,project,mojoInfo,listener,exception)) + throw new hudson.maven.agent.AbortException(r+" failed"); + if(exception!=null) + proxy.setResult(Result.FAILURE); + } + + void onReportGenerated(MavenProject project, MavenReportInfo report) throws IOException, InterruptedException, hudson.maven.agent.AbortException { + ModuleName name = new ModuleName(project); + MavenBuildProxy proxy = proxies.get(name); + for (MavenReporter r : reporters.get(name)) + if(!r.reportGenerated(proxy,project,report,listener)) + throw new hudson.maven.agent.AbortException(r+" failed"); + } + + + + private static final long serialVersionUID = 1L; + + @Override + public ClassLoader getClassLoader() + { + return new MaskingClassLoader( super.getClassLoader() ); + } +} \ No newline at end of file diff --git a/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java b/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java index e22ef30f52..96be09c870 100644 --- a/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java +++ b/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java @@ -28,7 +28,6 @@ import hudson.maven.MavenBuild.ProxyImpl2; import hudson.maven.util.ExecutionEventLogger; import hudson.model.BuildListener; import hudson.model.Executor; -import jenkins.model.Jenkins; import hudson.model.Result; import hudson.remoting.Channel; import hudson.remoting.DelegatingCallable; @@ -79,12 +78,6 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal */ private final boolean profile = MavenProcessFactory.profile; - /** - * Record all asynchronous executions as they are scheduled, - * to make sure they are all completed before we finish. - */ - protected transient /*final*/ List> futures; - HudsonMavenExecutionResult mavenExecutionResult; private final Map proxies; @@ -190,12 +183,6 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal } } - // since reporters might be from plugins, use the uberjar to resolve them. - public ClassLoader getClassLoader() { - return Jenkins.getInstance().getPluginManager().uberClassLoader; - } - - /** * Invoked after the maven has finished running, and in the master, not in the maven process. */ @@ -212,34 +199,10 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal } } - private class FilterImpl extends MavenBuildProxy2.Filter implements Serializable { - - private MavenBuildInformation mavenBuildInformation; - - private Channel channel; - - public FilterImpl(MavenBuildProxy2 core, MavenBuildInformation mavenBuildInformation, Channel channel) { - super(core); - this.mavenBuildInformation = mavenBuildInformation; - this.channel = channel; - } - - @Override - public void executeAsync(final BuildCallable program) throws IOException { - futures.add(channel.callAsync(new AsyncInvoker(core,program))); - } - - private static final long serialVersionUID = 1L; - - public MavenBuildInformation getMavenBuildInformation() - { - return mavenBuildInformation; - } - } - - private static final class MavenExecutionListener extends AbstractExecutionListener implements Serializable, ExecutionListener { + private static final long serialVersionUID = 4942789836756366116L; + private final Maven3Builder maven3Builder; /** diff --git a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java index c1336e75ff..b67b3f8d96 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -29,7 +29,6 @@ import hudson.AbortException; import hudson.EnvVars; import hudson.FilePath; import hudson.FilePath.FileCallable; -import hudson.Launcher; import hudson.Util; import hudson.maven.MavenBuild.ProxyImpl2; import hudson.maven.reporters.MavenAggregatedArtifactRecord; @@ -54,7 +53,6 @@ import hudson.model.Result; import hudson.model.Run; import hudson.model.StringParameterDefinition; import hudson.model.TaskListener; -import hudson.remoting.Channel; import hudson.remoting.VirtualChannel; import hudson.scm.ChangeLogSet; import hudson.tasks.BuildStep; @@ -63,14 +61,12 @@ import hudson.tasks.MailSender; import hudson.tasks.Maven.MavenInstallation; import hudson.util.ArgumentListBuilder; import hudson.util.IOUtils; -import hudson.util.MaskingClassLoader; import hudson.util.StreamTaskListener; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import java.io.PrintStream; -import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -90,13 +86,8 @@ import jenkins.model.Jenkins; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; -import org.apache.maven.BuildFailureException; import org.apache.maven.artifact.versioning.ComparableVersion; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.execution.ReactorManager; -import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.model.building.ModelBuildingRequest; -import org.apache.maven.monitor.event.EventDispatcher; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingException; import org.codehaus.plexus.util.PathTool; @@ -775,8 +766,8 @@ public class MavenModuleSetBuild extends AbstractMavenBuild proxies; - private final Map> reporters = new HashMap>(); - private final Map> executedMojos = new HashMap>(); - private long mojoStartTime; - - private MavenBuildProxy2 lastProxy; - - /** - * Kept so that we can finalize them in the end method. - */ - private final transient Map sourceProxies; - - public Builder(BuildListener listener,Map proxies, Collection modules, List goals, Map systemProps, MavenBuildInformation mavenBuildInformation) { - super(listener,goals,systemProps); - this.sourceProxies = proxies; - this.proxies = new HashMap(proxies); - for (Entry e : this.proxies.entrySet()) - e.setValue(new FilterImpl(e.getValue(), mavenBuildInformation)); - - for (MavenModule m : modules) - reporters.put(m.getModuleName(),m.createReporters()); - } - - private class FilterImpl extends MavenBuildProxy2.Filter implements Serializable { - - private MavenBuildInformation mavenBuildInformation; - - public FilterImpl(MavenBuildProxy2 core, MavenBuildInformation mavenBuildInformation) { - super(core); - this.mavenBuildInformation = mavenBuildInformation; - } - - @Override - public void executeAsync(final BuildCallable program) throws IOException { - futures.add(Channel.current().callAsync(new AsyncInvoker(core,program))); - } - - public MavenBuildInformation getMavenBuildInformation() { - return mavenBuildInformation; - } - - private static final long serialVersionUID = 1L; - - - } - - /** - * Invoked after the maven has finished running, and in the master, not in the maven process. - */ - void end(Launcher launcher) throws IOException, InterruptedException { - for (Map.Entry e : sourceProxies.entrySet()) { - ProxyImpl2 p = e.getValue(); - for (MavenReporter r : reporters.get(e.getKey())) { - // we'd love to do this when the module build ends, but doing so requires - // we know how many task segments are in the current build. - r.end(p.owner(),launcher,listener); - p.appendLastLog(); - } - p.close(); - } - } - - @Override - public Result call() throws IOException { - try { - if (debug) { - listener.getLogger().println("Builder extends MavenBuilder in call " + Thread.currentThread().getContextClassLoader()); - } - return super.call(); - } finally { - if(lastProxy!=null) - lastProxy.appendLastLog(); - } - } - - - @Override - void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException { - // set all modules which are not actually being build (in incremental builds) to NOT_BUILD - - List projects = rm.getSortedProjects(); - Set buildingProjects = new HashSet(); - for (MavenProject p : projects) { - buildingProjects.add(new ModuleName(p)); - } - - for (Entry e : this.proxies.entrySet()) { - if (! buildingProjects.contains(e.getKey())) { - MavenBuildProxy2 proxy = e.getValue(); - proxy.start(); - proxy.setResult(Result.NOT_BUILT); - proxy.end(); - } - } - } - - 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 { - ModuleName name = new ModuleName(project); - MavenBuildProxy2 proxy = proxies.get(name); - listener.getLogger().flush(); // make sure the data until here are all written - proxy.start(); - for (MavenReporter r : reporters.get(name)) - if(!r.preBuild(proxy,project,listener)) - throw new hudson.maven.agent.AbortException(r+" failed"); - } - - void postModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException { - ModuleName name = new ModuleName(project); - MavenBuildProxy2 proxy = proxies.get(name); - List rs = reporters.get(name); - if(rs==null) { // probe for issue #906 - throw new AssertionError("reporters.get("+name+")==null. reporters="+reporters+" proxies="+proxies); - } - for (MavenReporter r : rs) - if(!r.postBuild(proxy,project,listener)) - throw new hudson.maven.agent.AbortException(r+" failed"); - proxy.setExecutedMojos(executedMojos.get(name)); - listener.getLogger().flush(); // make sure the data until here are all written - proxy.end(); - lastProxy = proxy; - } - - void preExecute(MavenProject project, MojoInfo mojoInfo) throws IOException, InterruptedException, hudson.maven.agent.AbortException { - ModuleName name = new ModuleName(project); - MavenBuildProxy proxy = proxies.get(name); - for (MavenReporter r : reporters.get(name)) - if(!r.preExecute(proxy,project,mojoInfo,listener)) - throw new hudson.maven.agent.AbortException(r+" failed"); - - mojoStartTime = System.currentTimeMillis(); - } - - void postExecute(MavenProject project, MojoInfo mojoInfo, Exception exception) throws IOException, InterruptedException, hudson.maven.agent.AbortException { - ModuleName name = new ModuleName(project); - - List mojoList = executedMojos.get(name); - if(mojoList==null) - executedMojos.put(name,mojoList=new ArrayList()); - mojoList.add(new ExecutedMojo(mojoInfo,System.currentTimeMillis()-mojoStartTime)); - - MavenBuildProxy2 proxy = proxies.get(name); - for (MavenReporter r : reporters.get(name)) - if(!r.postExecute(proxy,project,mojoInfo,listener,exception)) - throw new hudson.maven.agent.AbortException(r+" failed"); - if(exception!=null) - proxy.setResult(Result.FAILURE); - } - - void onReportGenerated(MavenProject project, MavenReportInfo report) throws IOException, InterruptedException, hudson.maven.agent.AbortException { - ModuleName name = new ModuleName(project); - MavenBuildProxy proxy = proxies.get(name); - for (MavenReporter r : reporters.get(name)) - if(!r.reportGenerated(proxy,project,report,listener)) - throw new hudson.maven.agent.AbortException(r+" failed"); - } - - - - private static final long serialVersionUID = 1L; - - @Override - public ClassLoader getClassLoader() - { - return new MaskingClassLoader( super.getClassLoader() ); - } - } - /** * Used to tunnel exception from Maven through remoting. */