提交 f0a330a5 编写于 作者: K kohsuke

improved the interception to make things look uniform with Mojo interception.

Turns out MavenReport is really always a Mojo anyway.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@10935 71c3de6d-444a-0410-be80-ed276b4c234a
上级 4c00dac6
......@@ -15,7 +15,6 @@ import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
import hudson.util.ArgumentListBuilder;
import org.apache.maven.BuildFailureException;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.lifecycle.LifecycleExecutionException;
......@@ -243,7 +242,7 @@ public class MavenBuild extends AbstractBuild<MavenModule,MavenBuild> {
}
@Override
void onReportGenerated(MavenProject project, MavenReport report) throws IOException, InterruptedException, AbortException {
void onReportGenerated(MavenProject project, MavenReportInfo report) throws IOException, InterruptedException, AbortException {
for (MavenReporter r : reporters)
if(!r.reportGenerated(buildProxy,project,report,listener))
throw new AbortException(r+" failed");
......
......@@ -116,7 +116,7 @@ public abstract class MavenBuilder implements DelegatingCallable<Result,IOExcept
/**
* Called after a {@link MavenReport} is successfully generated.
*/
abstract void onReportGenerated(MavenProject project, MavenReport report) throws IOException, InterruptedException, AbortException;
abstract void onReportGenerated(MavenProject project, MavenReportInfo report) throws IOException, InterruptedException, AbortException;
/**
* This code is executed inside the maven jail process.
......@@ -257,9 +257,9 @@ public abstract class MavenBuilder implements DelegatingCallable<Result,IOExcept
overheadTime += System.nanoTime()-startTime;
}
public void onReportGenerated(MavenReport report) throws InterruptedException, IOException {
public void onReportGenerated(MavenReport report, MojoExecution mojoExecution, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException, InterruptedException {
long startTime = System.nanoTime();
listener.onReportGenerated(lastModule,report);
listener.onReportGenerated(lastModule,new MavenReportInfo(mojoExecution,report,mergedConfig,eval));
overheadTime += System.nanoTime()-startTime;
}
......
......@@ -22,7 +22,6 @@ import hudson.remoting.VirtualChannel;
import hudson.util.ArgumentListBuilder;
import hudson.util.StreamTaskListener;
import org.apache.maven.BuildFailureException;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.embedder.MavenEmbedderException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
......@@ -593,7 +592,7 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
proxy.setResult(Result.FAILURE);
}
void onReportGenerated(MavenProject project, MavenReport report) throws IOException, InterruptedException, hudson.maven.agent.AbortException {
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))
......
package hudson.maven;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.Mojo;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
/**
* Information about {@link MavenReport} that was executed.
*
* <p>
* Since {@link MavenReport} is always a {@link Mojo} (even though the interface
* inheritance is not explicitly defined), this class extends from {@link MojoInfo}.
*
* <p>
* This object provides additional convenience methods that only make sense for {@link MavenReport}.
*
* @author Kohsuke Kawaguchi
* @see MojoInfo
*/
public final class MavenReportInfo extends MojoInfo {
/**
* The fully-populated {@link MavenReport} object. The same object as
* {@link #mojo} but in the right type. Never null.
*/
public final MavenReport report;
public MavenReportInfo(MojoExecution mojoExecution, MavenReport mojo, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator) {
super(mojoExecution, (Mojo)mojo, configuration, expressionEvaluator);
this.report = mojo;
}
}
......@@ -215,7 +215,7 @@ public abstract class MavenReporter implements Describable<MavenReporter>, Exten
* See {@link #preBuild}
* @since 1.237
*/
public boolean reportGenerated(MavenBuildProxy build, MavenProject pom, MavenReport report, BuildListener listener) throws InterruptedException, IOException {
public boolean reportGenerated(MavenBuildProxy build, MavenProject pom, MavenReportInfo report, BuildListener listener) throws InterruptedException, IOException {
return true;
}
......
......@@ -28,8 +28,9 @@ import hudson.util.InvocationInterceptor;
*
* @author Kohsuke Kawaguchi
* @see MavenReporter
* @see MavenReportInfo
*/
public final class MojoInfo {
public class MojoInfo {
/**
* Object from Maven that describes the Mojo to be executed.
*/
......
......@@ -9,11 +9,13 @@ import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
import hudson.maven.MavenBuild;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenReportInfo;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.JavadocArchiver.JavadocAction;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReport;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import java.io.File;
......@@ -75,6 +77,10 @@ public class MavenJavadocArchiver extends MavenReporter {
return true;
}
@Override
public boolean reportGenerated(MavenBuildProxy build, MavenProject pom, MavenReportInfo report, BuildListener listener) throws InterruptedException, IOException {
return true;
}
public Action getProjectAction(MavenModule project) {
return new JavadocAction(project);
......
......@@ -34,7 +34,7 @@ public class RunCommand implements Callable {
System.out.println("==== "+exec.getMojoDescriptor().getGoal());
}
public void onReportGenerated(MavenReport report) {
public void onReportGenerated(MavenReport report, MojoExecution mojoExecution, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException, InterruptedException {
System.out.println("//// "+report);
}
});
......
......@@ -96,54 +96,73 @@ public class PluginManagerInterceptor extends DefaultPluginManager {
};
}
/**
* Intercepts the {@link Mojo} configuration and grabs some key Maven objects that are used for configuration,
* then call {@link #pre(Object, PlexusConfiguration, ExpressionEvaluator)} to provide an opportunity
* to alter the configuration.
*/
private abstract class MojoIntercepter extends ComponentConfiguratorFilter {
// these are the key objects involved in configuring a mojo
PlexusConfiguration config;
ExpressionEvaluator eval;
Mojo mojo;
MojoIntercepter() {
super(null);
// it is the caller's responsibility to set 'configuratorFilter' to null when the interception is over.
configuratorFilter = this;
}
@Override
public void configureComponent(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm, ConfigurationListener configListener) throws ComponentConfigurationException {
try {
this.config = configuration;
this.eval = expressionEvaluator;
this.mojo = (Mojo)component;
pre(component, configuration, expressionEvaluator);
super.configureComponent(component, configuration, expressionEvaluator, containerRealm, configListener);
} catch (IOException e) {
throw new ComponentConfigurationException(e);
} catch (InterruptedException e) {
// orderly abort
throw new AbortException("Execution aborted",e);
}
}
protected abstract void pre(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator) throws IOException, InterruptedException;
}
@Override
public void executeMojo(final MavenProject project, final MojoExecution mojoExecution, MavenSession session) throws ArtifactResolutionException, MojoExecutionException, MojoFailureException, ArtifactNotFoundException, InvalidDependencyVersionException, PluginManagerException, PluginConfigurationException {
class MojoConfig {
PlexusConfiguration config;
ExpressionEvaluator eval;
Mojo mojo;
class MojoIntercepterImpl extends MojoIntercepter {
@Override
protected void pre(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator) throws IOException, InterruptedException {
if(listener!=null)
// this lets preExecute a chance to modify the mojo configuration
listener.preExecute(project,mojoExecution, (Mojo)component, configuration,expressionEvaluator);
}
void callPost(Exception exception) throws IOException, InterruptedException {
if(listener!=null)
listener.postExecute(project,mojoExecution, mojo, config,eval,exception);
listener.postExecute(project,mojoExecution,mojo,config,eval,exception);
}
}
final MojoConfig config = new MojoConfig();
// prepare interception of ComponentConfigurator, so that we can get the final PlexusConfiguration object
// representing the configuration before Mojo object is filled with that.
configuratorFilter = new ComponentConfiguratorFilter(null) {
@Override
public void configureComponent(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm, ConfigurationListener configListener) throws ComponentConfigurationException {
try {
config.config = configuration;
config.eval = expressionEvaluator;
config.mojo = (Mojo)component;
if(listener!=null)
// this lets preExecute a chance to modify the mojo configuration
listener.preExecute(project,mojoExecution, (Mojo)component, configuration,expressionEvaluator);
super.configureComponent(component, configuration, expressionEvaluator, containerRealm, configListener);
} catch (IOException e) {
throw new ComponentConfigurationException(e);
} catch (InterruptedException e) {
// orderly abort
throw new AbortException("Execution aborted",e);
}
}
};
MojoIntercepterImpl interceptor = new MojoIntercepterImpl();
try {
try {
// inside the executeMojo but before the mojo actually gets executed,
// we should be able to trap the mojo configuration.
super.executeMojo(project, mojoExecution, session);
config.callPost(null);
interceptor.callPost(null);
} catch (MojoExecutionException e) {
config.callPost(e);
interceptor.callPost(e);
throw e;
} catch (MojoFailureException e) {
config.callPost(e);
interceptor.callPost(e);
throw e;
}
} catch (InterruptedException e) {
......@@ -162,8 +181,19 @@ public class PluginManagerInterceptor extends DefaultPluginManager {
* of certain reporting.
*/
@Override
public MavenReport getReport(MavenProject project, MojoExecution mojoExecution, MavenSession session) throws ArtifactNotFoundException, PluginConfigurationException, PluginManagerException, ArtifactResolutionException {
MavenReport r = super.getReport(project, mojoExecution, session);
public MavenReport getReport(MavenProject project, final MojoExecution mojoExecution, MavenSession session) throws ArtifactNotFoundException, PluginConfigurationException, PluginManagerException, ArtifactResolutionException {
// intercept the MavenReport object creation.
final MojoIntercepter interceptor = new MojoIntercepter() {
protected void pre(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator) throws IOException, InterruptedException {
}
};
MavenReport r;
try {
r = super.getReport(project, mojoExecution, session);
} finally {
configuratorFilter = null;
}
if(r==null) return null;
r = new ComponentInterceptor<MavenReport>() {
......@@ -175,7 +205,7 @@ public class PluginManagerInterceptor extends DefaultPluginManager {
Object r = super.invoke(proxy, method, args);
// on successul execution of the generate method, raise an event
try {
listener.onReportGenerated(delegate);
listener.onReportGenerated(delegate,mojoExecution,interceptor.config,interceptor.eval);
} catch (InterruptedException e) {
// orderly abort
throw new AbortException("Execution aborted",e);
......
......@@ -55,5 +55,5 @@ public interface PluginManagerListener {
* @param report
* The {@link MavenReport} object that just successfully completed.
*/
void onReportGenerated(MavenReport report) throws IOException, InterruptedException;
void onReportGenerated(MavenReport report, MojoExecution mojoExecution, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException, InterruptedException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册