From 9ecc4a9a7a6f7794381e4e2ed34d077ce584fa32 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 6 Nov 2007 15:12:45 +0000 Subject: [PATCH] Fixed #980. added one more phase to the build, namely the cleanUp method. This is where you can perform things after the build is marked as completed. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@5745 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/maven/MavenBuild.java | 5 ++ .../hudson/maven/MavenModuleSetBuild.java | 7 ++- .../main/java/hudson/model/AbstractBuild.java | 4 ++ core/src/main/java/hudson/model/Build.java | 21 +++++-- .../main/java/hudson/model/ExternalRun.java | 8 +++ core/src/main/java/hudson/model/Run.java | 57 ++++++++++++++----- .../main/java/hudson/tasks/BuildTrigger.java | 5 ++ .../src/main/java/hudson/tasks/Publisher.java | 30 ++++++++++ 8 files changed, 116 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/hudson/maven/MavenBuild.java b/core/src/main/java/hudson/maven/MavenBuild.java index f7c5597cd4..012f44dd6e 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 04f48efd21..5e5e7ce654 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 17a301ef9c..2df6d6b773 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 4de20c282c..9154d9d748 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 0ee95eafec..d955800522 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; + } } -- GitLab