BuildWrapper.java 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package hudson.tasks;

import hudson.ExtensionPoint;
import hudson.Launcher;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Describable;
import hudson.model.Project;

import java.io.IOException;
K
kohsuke 已提交
11
import java.util.Map;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/**
 * Pluggability point for performing pre/post actions for the build process.
 *
 * <p>
 * <b>STILL EXPERIMENTAL. SUBJECT TO CHANGE</b>
 *
 * <p>
 * This extension point enables a plugin to set up / tear down additional
 * services needed to perform a build, such as setting up local X display,
 * or launching and stopping application servers for testing.
 *
 * <p>
 * An instance of {@link BuildWrapper} is associated with a {@link Project}
 * with configuration information as its state. An instance is persisted
 * along with {@link Project}.
 *
 * <p>
30
 * The {@link #setUp(Build,Launcher,BuildListener)} method is invoked for each build.
31 32 33 34 35
 *
 * @author Kohsuke Kawaguchi
 */
public abstract class BuildWrapper implements ExtensionPoint, Describable<BuildWrapper> {
    /**
36
     * Represents the environment set up by {@link BuildWrapper#setUp(Build,Launcher,BuildListener)}.
37 38 39 40 41
     *
     * <p>
     * It is expected that the subclasses of {@link BuildWrapper} extends this
     * class and implements its own semantics.
     */
42
    public abstract class Environment {
43 44
        /**
         * Adds environmental variables for the builds to the given map.
K
kohsuke 已提交
45 46 47 48 49 50 51 52 53 54 55 56
         *
         * <p>
         * If the {@link Environment} object wants to pass in information
         * to the build that runs, it can do so by exporting additional
         * environment variables to the map.
         *
         * <p>
         * When this method is invoked, the map already contains the
         * current "planned export" list.
         *
         * @param env
         *      never null. 
57 58 59 60 61 62 63 64 65 66 67 68 69 70
         */
        public void buildEnvVars(Map<String,String> env) {
            // no-op by default
        }

        /**
         * Runs after the {@link Builder} completes, and performs a tear down.
         *
         * <p>
         * This method is invoked even when the build failed, so that the
         * clean up operation can be performed regardless of the build result
         * (for example, you'll want to stop application server even if a build
         * fails.)
         *
K
kohsuke 已提交
71 72 73 74
         * @param build
         *      The same {@link Build} object given to the set up method.
         * @param listener
         *      The same {@link BuildListener} object given to the set up method.
75 76 77
         * @return
         *      true if the build can continue, false if there was an error
         *      and the build needs to be aborted.
K
kohsuke 已提交
78 79 80
         * @throws IOException
         *      terminates the build abnormally. Hudson will handle the exception
         *      and reports a nice error message.
81
         */
82
        public abstract boolean tearDown( Build build, BuildListener listener ) throws IOException;
83 84 85 86 87
    }

    /**
     * Runs before the {@link Builder} runs, and performs a set up.
     *
K
kohsuke 已提交
88 89 90 91 92 93 94 95 96
     * @param build
     *      The build in progress for which an {@link Environment} object is created.
     *      Never null.
     * @param launcher
     *      This launcher can be used to launch processes for this build.
     *      If the build runs remotely, launcher will also run a job on that remote machine.
     *      Never null.
     * @param listener
     *      Can be used to send any message.
97 98 99
     * @return
     *      non-null if the build can continue, null if there was an error
     *      and the build needs to be aborted.
K
kohsuke 已提交
100 101 102
     * @throws IOException
     *      terminates the build abnormally. Hudson will handle the exception
     *      and reports a nice error message.
103
     */
104
    public abstract Environment setUp( Build build, Launcher launcher, BuildListener listener ) throws IOException;
105
}