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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5 * Red Hat, Inc., Victor Glushenkov, Alan Harder, Olivier Lamy, Christoph Kutzinski
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package hudson.maven;
26
27 import hudson.maven.MavenBuild.ProxyImpl2;
28 import hudson.maven.reporters.TestFailureDetector;
29 import hudson.model.BuildListener;
30 import hudson.model.Result;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.Map.Entry;
40 import java.util.concurrent.atomic.AtomicBoolean;
41
42 import org.apache.maven.BuildFailureException;
43 import org.apache.maven.execution.MavenSession;
44 import org.apache.maven.execution.ReactorManager;
45 import org.apache.maven.lifecycle.LifecycleExecutionException;
46 import org.apache.maven.monitor.event.EventDispatcher;
47 import org.apache.maven.project.MavenProject;
48
49 /**
50 * Runs Maven and builds the project.
51 *
52 * This is only used for
53 * {@link MavenModuleSet#isAggregatorStyleBuild() the aggregator style build}.
54 */
55 @SuppressWarnings("deprecation") // as we're restricted to Maven 2.x API here, but compile against Maven 3.x, we cannot avoid deprecations
56 final class Maven2Builder extends MavenBuilder {
57 private final Map<ModuleName,List<ExecutedMojo>> executedMojos = new HashMap<ModuleName,List<ExecutedMojo>>();
58 private long mojoStartTime;
59
60 private MavenBuildProxy2 lastProxy;
61 private final AtomicBoolean hasTestFailures = new AtomicBoolean();
62
63
64 public Maven2Builder(BuildListener listener,Map<ModuleName,ProxyImpl2> proxies, Collection<MavenModule> modules, List<String> goals, Map<String,String> systemProps, MavenBuildInformation mavenBuildInformation) {
65 super(listener,modules,goals,systemProps);
66 this.sourceProxies.putAll(proxies);
67 this.proxies = new HashMap<ModuleName, FilterImpl>();
68 for (Entry<ModuleName,ProxyImpl2> e : this.sourceProxies.entrySet()) {
69 this.proxies.put(e.getKey(), new FilterImpl(e.getValue(), mavenBuildInformation));
70 }
71 }
72
73 @Override
74 public Result call() throws IOException {
75 try {
76 if (MavenModuleSetBuild.debug) {
77 listener.getLogger().println("Builder extends MavenBuilder in call " + Thread.currentThread().getContextClassLoader());
78 }
79 return super.call();
80 } finally {
81 if(lastProxy!=null)
82 lastProxy.appendLastLog();
83 }
84 }
85
86
87 @Override
88 void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException {
89 // set all modules which are not actually being build (in incremental builds) to NOT_BUILD
90
91 List<MavenProject> projects = rm.getSortedProjects();
92 Set<ModuleName> buildingProjects = new HashSet<ModuleName>();
93 for (MavenProject p : projects) {
94 buildingProjects.add(new ModuleName(p));
95 }
96
97 for (Entry<ModuleName,FilterImpl> e : this.proxies.entrySet()) {
98 if (! buildingProjects.contains(e.getKey())) {
99 MavenBuildProxy2 proxy = e.getValue();
100 proxy.start();
101 proxy.setResult(Result.NOT_BUILT);
102 proxy.end();
103 }
104 }
105 }
106
107 void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException, IOException, InterruptedException {
108 // TODO
109 }
110
111 void preModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException {
112 ModuleName name = new ModuleName(project);
113 MavenBuildProxy2 proxy = proxies.get(name);
114 listener.getLogger().flush(); // make sure the data until here are all written
115 proxy.start();
116 for (MavenReporter r : reporters.get(name))
117 if(!r.preBuild(proxy,project,listener))
118 throw new hudson.maven.agent.AbortException(r+" failed");
119 }
120
121 void postModule(MavenProject project) throws InterruptedException, IOException, hudson.maven.agent.AbortException {
122 ModuleName name = new ModuleName(project);
123 MavenBuildProxy2 proxy = proxies.get(name);
124 List<MavenReporter> rs = reporters.get(name);
125 if(rs==null) { // probe for issue #906
126 throw new AssertionError("reporters.get("+name+")==null. reporters="+reporters+" proxies="+proxies);
127 }
128 for (MavenReporter r : rs)
129 if(!r.postBuild(proxy,project,listener))
130 throw new hudson.maven.agent.AbortException(r+" failed");
131 proxy.setExecutedMojos(executedMojos.get(name));
132 listener.getLogger().flush(); // make sure the data until here are all written
133 proxy.end();
134 lastProxy = proxy;
135 }
136
137 void preExecute(MavenProject project, MojoInfo mojoInfo) throws IOException, InterruptedException, hudson.maven.agent.AbortException {
138 ModuleName name = new ModuleName(project);
139 MavenBuildProxy proxy = proxies.get(name);
140 for (MavenReporter r : reporters.get(name))
141 if(!r.preExecute(proxy,project,mojoInfo,listener))
142 throw new hudson.maven.agent.AbortException(r+" failed");
143
144 mojoStartTime = System.currentTimeMillis();
145 }
146
147 void postExecute(MavenProject project, MojoInfo mojoInfo, Exception exception) throws IOException, InterruptedException, hudson.maven.agent.AbortException {
148 ModuleName name = new ModuleName(project);
149
150 List<ExecutedMojo> mojoList = executedMojos.get(name);
151 if(mojoList==null)
152 executedMojos.put(name,mojoList=new ArrayList<ExecutedMojo>());
153 mojoList.add(new ExecutedMojo(mojoInfo,System.currentTimeMillis()-mojoStartTime));
154
155 MavenBuildProxy2 proxy = proxies.get(name);
156 for (MavenReporter r : reporters.get(name)){
157 if(!r.postExecute(proxy,project,mojoInfo,listener,exception)) {
158 throw new hudson.maven.agent.AbortException(r+" failed");
159 } else if (r instanceof TestFailureDetector) {
160 if(((TestFailureDetector) r).hasTestFailures()) {
161 hasTestFailures.compareAndSet(false, true);
162 }
163 }
164 }
165 if(exception!=null)
166 proxy.setResult(Result.FAILURE);
167 }
168
169 void onReportGenerated(MavenProject project, MavenReportInfo report) throws IOException, InterruptedException, hudson.maven.agent.AbortException {
170 ModuleName name = new ModuleName(project);
171 MavenBuildProxy proxy = proxies.get(name);
172 for (MavenReporter r : reporters.get(name))
173 if(!r.reportGenerated(proxy,project,report,listener))
174 throw new hudson.maven.agent.AbortException(r+" failed");
175 }
176
177 @Override
178 public boolean hasBuildFailures() {
179 return hasTestFailures.get();
180 }
181
182 private static final long serialVersionUID = 1L;
183 }