File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\Build.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Martin Eigenbrodt
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.Launcher;
27 import hudson.tasks.BuildStep;
28 import hudson.tasks.BuildWrapper;
29 import hudson.tasks.Builder;
30 import hudson.tasks.Recorder;
31 import hudson.tasks.Notifier;
32 import org.kohsuke.accmod.Restricted;
33 import org.kohsuke.accmod.restrictions.NoExternalUse;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.text.MessageFormat;
38 import java.util.ArrayList;
39 import java.util.Calendar;
40 import java.util.Collection;
41 import java.util.List;
42 import java.util.logging.Logger;
43
44 import static hudson.model.Result.ABORTED;
45 import static hudson.model.Result.FAILURE;
46
47 /**
48 * A build of a {@link Project}.
49 *
50 * <h2>Steps of a build</h2>
51 * <p>
52 * Roughly speaking, a {@link Build} goes through the following stages:
53 *
54 * <dl>
55 * <dt>SCM checkout
56 * <dd>Hudson decides which directory to use for a build, then the source code is checked out
57 *
58 * <dt>Pre-build steps
59 * <dd>Everyone gets their {@link BuildStep#prebuild(AbstractBuild, BuildListener)} invoked
60 * to indicate that the build is starting
61 *
62 * <dt>Build wrapper set up
63 * <dd>{@link BuildWrapper#setUp(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
64 * to prepare an environment for the build.
65 *
66 * <dt>Builder runs
67 * <dd>{@link Builder#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is where
68 * things that are useful to users happen, like calling Ant, Make, etc.
69 *
70 * <dt>Recorder runs
71 * <dd>{@link Recorder#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
72 * to record the output from the build, such as test results.
73 *
74 * <dt>Notifier runs
75 * <dd>{@link Notifier#perform(AbstractBuild, Launcher, BuildListener)} is invoked. This is normally
76 * to send out notifications, based on the results determined so far.
77 * </dl>
78 *
79 * <p>
80 * And beyond that, the build is considered complete, and from then on {@link Build} object is there to
81 * keep the record of what happened in this build.
82 *
83 * @author Kohsuke Kawaguchi
84 */
85 public abstract class Build <P extends Project<P,B>,B extends Build<P,B>>
86 extends AbstractBuild<P,B> {
87
88 /**
89 * Creates a new build.
90 */
91 protected Build(P project) throws IOException {
92 super(project);
93 }
94
95 protected Build(P job, Calendar timestamp) {
96 super(job, timestamp);
97 }
98
99 /**
100 * Loads a build from a log file.
101 */
102 protected Build(P project, File buildDir) throws IOException {
103 super(project,buildDir);
104 }
105
106 //
107 //
108 // actions
109 //
110 //
111 @Override
112 public void run() {
113 execute(createRunner());
114 }
115
116 /**
117 * @deprecated as of 1.467
118 * Override the {@link #run()} method by calling {@link #execute(RunExecution)} with
119 * proper execution object.
120 */
121 @Restricted(NoExternalUse.class)
122 protected Runner createRunner() {
123 return new BuildExecution();
124 }
125
126 /**
127 * @deprecated as of 1.467
128 * Please use {@link BuildExecution}
129 */
130 protected class RunnerImpl extends BuildExecution {
131 }
132
133 protected class BuildExecution extends AbstractRunner {
134 /*
135 Some plugins might depend on this instance castable to Runner, so we need to use
136 deprecated class here.
137 */
138
139 protected Result doRun(BuildListener listener) throws Exception {
140 if(!preBuild(listener,project.getBuilders()))
141 return FAILURE;
142 if(!preBuild(listener,project.getPublishersList()))
143 return FAILURE;
144
145 Result r = null;
146 try {
147 List<BuildWrapper> wrappers = new ArrayList<BuildWrapper>(project.getBuildWrappers().values());
148
149 ParametersAction parameters = getAction(ParametersAction.class);
150 if (parameters != null)
151 parameters.createBuildWrappers(Build.this,wrappers);
152
153 for( BuildWrapper w : wrappers ) {
154 Environment e = w.setUp((AbstractBuild<?,?>)Build.this, launcher, listener);
155 if(e==null)
156 return (r = FAILURE);
157 buildEnvironments.add(e);
158 }
159
160 if(!build(listener,project.getBuilders()))
161 r = FAILURE;
162 } catch (InterruptedException e) {
163 r = Executor.currentExecutor().abortResult();
164 // not calling Executor.recordCauseOfInterruption here. We do that where this exception is consumed.
165 throw e;
166 } finally {
167 if (r != null) setResult(r);
168 // tear down in reverse order
169 boolean failed=false;
170 for( int i=buildEnvironments.size()-1; i>=0; i-- ) {
171 if (!buildEnvironments.get(i).tearDown(Build.this,listener)) {
172 failed=true;
173 }
174 }
175 // WARNING The return in the finally clause will trump any return before
176 if (failed) return FAILURE;
177 }
178
179 return r;
180 }
181
182 public void post2(BuildListener listener) throws IOException, InterruptedException {
183 if (!performAllBuildSteps(listener, project.getPublishersList(), true))
184 setResult(FAILURE);
185 if (!performAllBuildSteps(listener, project.getProperties(), true))
186 setResult(FAILURE);
187 }
188
189 @Override
190 public void cleanUp(BuildListener listener) throws Exception {
191 // at this point it's too late to mark the build as a failure, so ignore return value.
192 performAllBuildSteps(listener, project.getPublishersList(), false);
193 performAllBuildSteps(listener, project.getProperties(), false);
194 super.cleanUp(listener);
195 }
196
197 private boolean build(BuildListener listener, Collection<Builder> steps) throws IOException, InterruptedException {
198 for( BuildStep bs : steps ) {
199 if(!perform(bs,listener)) {
200 LOGGER.fine(MessageFormat.format("{0} : {1} failed", Build.this.toString(), bs));
201 return false;
202 }
203 if (getExecutor().isInterrupted()) {
204 // someone asked build interruption, let stop the build before trying to run another build step
205 throw new InterruptedException();
206 }
207 }
208 return true;
209 }
210 }
211
212 private static final Logger LOGGER = Logger.getLogger(Build.class.getName());
213 }
214