diff --git a/core/src/main/java/hudson/maven/MavenBuild.java b/core/src/main/java/hudson/maven/MavenBuild.java index f7c5597cd479d695939c4b9e47d2a3dada030885..012f44dd6e07ff121ba15c473c5ac868bd8c8b5f 100644 --- a/core/src/main/java/hudson/maven/MavenBuild.java +++ b/core/src/main/java/hudson/maven/MavenBuild.java @@ -290,6 +290,9 @@ public class MavenBuild extends AbstractBuild { public void post(BuildListener listener) { } + + public void cleanUp(BuildListener listener) { + } }); } } @@ -347,7 +350,9 @@ public class MavenBuild extends AbstractBuild { public void post2(BuildListener listener) throws Exception { for (MavenReporter reporter : reporters) reporter.end(MavenBuild.this,launcher,listener); + } + public void cleanUp(BuildListener listener) throws Exception { if(getResult().isBetterOrEqualTo(Result.SUCCESS)) scheduleDownstreamBuilds(listener,new HashSet()); } diff --git a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java index 04f48efd2181b8410f21cb8c696418f03c321019..5e5e7ce654cae1f48178cd1cbebd487877f5d530 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -333,7 +333,12 @@ public final class MavenModuleSetBuild extends AbstractBuild,R extends Abs culprits = r; } } + + public void cleanUp(BuildListener listener) throws Exception { + // default is no-op + } } /** diff --git a/core/src/main/java/hudson/model/Build.java b/core/src/main/java/hudson/model/Build.java index 17a301ef9c403488020c52de7203b474a4c22c81..2df6d6b7738f7a740821a68d2324299f4b68b0d8 100644 --- a/core/src/main/java/hudson/model/Build.java +++ b/core/src/main/java/hudson/model/Build.java @@ -4,6 +4,7 @@ import hudson.tasks.BuildStep; import hudson.tasks.BuildWrapper; import hudson.tasks.BuildWrapper.Environment; import hudson.tasks.Builder; +import hudson.tasks.Publisher; import hudson.triggers.SCMTrigger; import java.io.File; @@ -133,11 +134,21 @@ public abstract class Build

,B extends Build> } public void post2(BuildListener listener) throws IOException, InterruptedException { - // run all of them even if one of them failed - for( BuildStep bs : project.getPublishers().values() ) - bs.perform(Build.this, launcher, listener); - for( BuildStep bs : project.getProperties().values() ) - bs.perform(Build.this, launcher, listener); + performAllBuildStep(listener, project.getPublishers(),true); + performAllBuildStep(listener, project.getProperties(),true); + } + + public void cleanUp(BuildListener listener) throws Exception { + performAllBuildStep(listener, project.getPublishers(),false); + performAllBuildStep(listener, project.getProperties(),false); + } + + // run all of them even if one of them failed + private void performAllBuildStep(BuildListener listener, Map buildSteps, boolean phase) throws InterruptedException, IOException { + for( BuildStep bs : buildSteps.values() ) { + if( (bs instanceof Publisher && ((Publisher)bs).needsToRunAfterFinalized()) ^ phase) + bs.perform(Build.this, launcher, listener); + } } private boolean build(BuildListener listener, Map steps) throws IOException, InterruptedException { diff --git a/core/src/main/java/hudson/model/ExternalRun.java b/core/src/main/java/hudson/model/ExternalRun.java index 4de20c282c1e7eb496c80892f5c6b1791d193cd7..9154d9d74889d2543928aef6438e5c26cd2f212e 100644 --- a/core/src/main/java/hudson/model/ExternalRun.java +++ b/core/src/main/java/hudson/model/ExternalRun.java @@ -45,6 +45,10 @@ public class ExternalRun extends Run { public void post(BuildListener listener) { // do nothing } + + public void cleanUp(BuildListener listener) { + // do nothing + } }); } @@ -94,6 +98,10 @@ public class ExternalRun extends Run { public void post(BuildListener listener) { // do nothing } + + public void cleanUp(BuildListener listener) { + // do nothing + } }); if(duration[0]!=0) { diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index 0ee95eafec26543e8b6e53838199e25acf114466..d9558005229c87492159daee49704e7f9a7b0d10 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -569,7 +569,7 @@ public abstract class Run ,RunT extends Run * This method is called after the status of the build is determined. * This is a good opportunity to do notifications based on the result * of the build. When this method is called, the build is not really @@ -578,6 +578,18 @@ public abstract class Run ,RunT extends Run + * This method is called after {@link #post(BuildListener)}, + * after the build result is fully finalized. This is the point + * where the build is already considered completed. + *

+ * Among other things, this is often a necessary pre-condition + * before invoking other builds that depend on this build. + */ + void cleanUp(BuildListener listener) throws Exception; } /** @@ -591,14 +603,15 @@ public abstract class Run ,RunT extends Run,RunT extends Run + * The execution of normal {@link Publisher}s are considered within a part + * of the build. This allows publishers to mark the build as a failure, or + * to include their execution time in the total build time. + * + *

+ * So normally, that is the preferrable behavior, but in a few cases + * this is problematic. One of such cases is when a publisher needs to + * trigger other builds, whcih in turn need to see this build as a + * completed build. Those plugins that need to do this can return true + * from this method, so that the {@link #perform(AbstractBuild, Launcher, BuildListener)} + * method is called after the build is marked as completed. + * + *

+ * When {@link Publisher} behaves this way, note that they can no longer + * change the build status anymore. + * + * @author Kohsuke Kawaguchi + * @since 1.153 + */ + public boolean needsToRunAfterFinalized() { + return false; + } }