Lines 10-16
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.core.resources.ant; |
11 |
package org.eclipse.core.resources.ant; |
12 |
|
12 |
|
13 |
import java.util.Hashtable; |
13 |
import org.eclipse.core.runtime.IPath; |
|
|
14 |
|
15 |
import org.eclipse.core.resources.IResource; |
16 |
|
17 |
import org.apache.tools.ant.Project; |
18 |
|
19 |
import org.eclipse.core.resources.IMarker; |
20 |
|
21 |
import java.util.*; |
14 |
import org.apache.tools.ant.BuildException; |
22 |
import org.apache.tools.ant.BuildException; |
15 |
import org.apache.tools.ant.Task; |
23 |
import org.apache.tools.ant.Task; |
16 |
import org.eclipse.ant.core.AntCorePlugin; |
24 |
import org.eclipse.ant.core.AntCorePlugin; |
Lines 28-33
Link Here
|
28 |
private String builder; |
36 |
private String builder; |
29 |
private String project; |
37 |
private String project; |
30 |
private int kind = IncrementalProjectBuilder.INCREMENTAL_BUILD; |
38 |
private int kind = IncrementalProjectBuilder.INCREMENTAL_BUILD; |
|
|
39 |
private boolean failOnError = true; |
40 |
private boolean showProblems = true; |
41 |
private String errorProperty; |
31 |
|
42 |
|
32 |
/** |
43 |
/** |
33 |
* Unique identifier constant (value <code>"incremental"</code>) |
44 |
* Unique identifier constant (value <code>"incremental"</code>) |
Lines 71-90
Link Here
|
71 |
Hashtable references = getProject().getReferences(); |
82 |
Hashtable references = getProject().getReferences(); |
72 |
if (references != null) |
83 |
if (references != null) |
73 |
monitor = (IProgressMonitor) references.get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR); |
84 |
monitor = (IProgressMonitor) references.get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR); |
|
|
85 |
IResource rootResource; |
74 |
if (project == null) { |
86 |
if (project == null) { |
|
|
87 |
rootResource = ResourcesPlugin.getWorkspace().getRoot(); |
75 |
ResourcesPlugin.getWorkspace().build(kind, monitor); |
88 |
ResourcesPlugin.getWorkspace().build(kind, monitor); |
76 |
} else { |
89 |
} else { |
77 |
IProject targetProject = ResourcesPlugin.getWorkspace().getRoot().getProject(project); |
90 |
IProject targetProject = ResourcesPlugin.getWorkspace().getRoot().getProject(project); |
|
|
91 |
rootResource = targetProject; |
78 |
if (builder == null) |
92 |
if (builder == null) |
79 |
targetProject.build(kind, monitor); |
93 |
targetProject.build(kind, monitor); |
80 |
else |
94 |
else |
81 |
targetProject.build(kind, builder, null, monitor); |
95 |
targetProject.build(kind, builder, null, monitor); |
82 |
} |
96 |
} |
|
|
97 |
Problems problems = new Problems(rootResource); |
98 |
if (showProblems) { |
99 |
showProblems(problems); |
100 |
} |
101 |
if (errorProperty != null) { |
102 |
if (problems.getErrors().length > 0) { |
103 |
getProject().setProperty(errorProperty, "true"); //$NON-NLS-1$ |
104 |
} |
105 |
} |
106 |
if (failOnError) { |
107 |
if (problems.getErrors().length > 0) { |
108 |
throw new BuildException(); |
109 |
} |
110 |
} |
83 |
} catch (CoreException e) { |
111 |
} catch (CoreException e) { |
84 |
throw new BuildException(e); |
112 |
throw new BuildException(e); |
85 |
} |
113 |
} |
86 |
} |
114 |
} |
87 |
|
115 |
|
|
|
116 |
private void showProblems(Problems problems) throws CoreException { |
117 |
IMarker[] warningsAndErrors = problems.getWarningAndErrors(); |
118 |
for (int i=0; i<warningsAndErrors.length; ++i) { |
119 |
IMarker problem = warningsAndErrors[i]; |
120 |
int severity = ((Integer) problem.getAttribute(IMarker.SEVERITY)).intValue(); |
121 |
int logLevel = (severity == IMarker.SEVERITY_ERROR) ? Project.MSG_WARN : Project.MSG_ERR; |
122 |
getProject().log(formatProblemMessage(problem), logLevel); |
123 |
} |
124 |
} |
125 |
|
126 |
/** |
127 |
* Returns a string suitable for display that represents a build problem. |
128 |
* |
129 |
* @param problem the problem |
130 |
* @return a string suitable for display that represents the problem |
131 |
*/ |
132 |
private String formatProblemMessage(IMarker problem) { |
133 |
StringBuffer sb = new StringBuffer(); |
134 |
|
135 |
// start with "path:linenum: " |
136 |
IResource resource = problem.getResource(); |
137 |
if (resource != null) { |
138 |
IPath resourceLocation = resource.getLocation(); |
139 |
if (resourceLocation != null) { |
140 |
sb.append(resourceLocation.toOSString()); |
141 |
sb.append(':'); |
142 |
|
143 |
int lineNumber = problem.getAttribute(IMarker.LINE_NUMBER, -1); |
144 |
if (lineNumber != -1) { |
145 |
sb.append(lineNumber); |
146 |
sb.append(':'); |
147 |
} |
148 |
|
149 |
sb.append(' '); |
150 |
} |
151 |
} |
152 |
|
153 |
// if it is a warning, append "warning: " |
154 |
int severity = problem.getAttribute(IMarker.SEVERITY, -1); |
155 |
if (severity == IMarker.SEVERITY_WARNING) { |
156 |
sb.append("warning: "); //$NON-NLS-1$ |
157 |
} |
158 |
|
159 |
// append the problem's actual message |
160 |
String message = problem.getAttribute(IMarker.MESSAGE, null); |
161 |
if (message != null) { |
162 |
sb.append(message); |
163 |
} |
164 |
|
165 |
return sb.toString(); |
166 |
} |
167 |
|
88 |
/** |
168 |
/** |
89 |
* Sets the name of the receiver's builder. |
169 |
* Sets the name of the receiver's builder. |
90 |
* |
170 |
* |
Lines 95-101
Link Here
|
95 |
} |
175 |
} |
96 |
|
176 |
|
97 |
/** |
177 |
/** |
98 |
* Sets the receiver's kind> attribute. This value must be one |
178 |
* Sets the receiver's kind attribute. This value must be one |
99 |
* of: <code>IncrementalBuild.KIND_FULL</code>, |
179 |
* of: <code>IncrementalBuild.KIND_FULL</code>, |
100 |
* <code>IncrementalBuild.KIND_AUTO</code>, |
180 |
* <code>IncrementalBuild.KIND_AUTO</code>, |
101 |
* <code>IncrementalBuild.KIND_INCREMENTAL</code>, |
181 |
* <code>IncrementalBuild.KIND_INCREMENTAL</code>, |
Lines 122-125
Link Here
|
122 |
public void setProject(String value) { |
202 |
public void setProject(String value) { |
123 |
project = value; |
203 |
project = value; |
124 |
} |
204 |
} |
|
|
205 |
|
206 |
/** |
207 |
* Sets whether build errors will cause the build to fail; defaults to <code>true</code>. |
208 |
* |
209 |
* @param value whether build errors should cause the build to fail |
210 |
*/ |
211 |
public void setFailOnError(boolean value) { |
212 |
failOnError = value; |
213 |
} |
214 |
|
215 |
/** |
216 |
* Sets whether build errors and warnings should be displayed; defaults to <code>true</code>. |
217 |
* |
218 |
* @param value whether build errors and warnings should be displayed |
219 |
*/ |
220 |
public void setShowProblems(boolean value) { |
221 |
showProblems = value; |
222 |
} |
223 |
|
224 |
/** |
225 |
* The property to set (to the value "true") if the build fails. |
226 |
* |
227 |
* @param value the name of the Ant property |
228 |
*/ |
229 |
public void setErrorProperty(String value) { |
230 |
errorProperty = value; |
231 |
} |
232 |
|
233 |
/** |
234 |
* Helper class to keep track of build problems. |
235 |
*/ |
236 |
private static class Problems { |
237 |
private final IResource resource; |
238 |
private IMarker[] allProblems; |
239 |
private IMarker[] errors; |
240 |
private IMarker[] errorsAndWarnings; |
241 |
|
242 |
public Problems(IResource resource) { |
243 |
this.resource = resource; |
244 |
} |
245 |
|
246 |
/** |
247 |
* Returns all problem markers on the resource. |
248 |
* @throws CoreException |
249 |
*/ |
250 |
public IMarker[] getAllProblems() throws CoreException { |
251 |
if (allProblems == null) { |
252 |
allProblems = resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); |
253 |
} |
254 |
return allProblems; |
255 |
} |
256 |
|
257 |
/** |
258 |
* Returns all problem markers on the resource whose severity is <code>IMarker.SEVERITY_ERROR</code>. |
259 |
* @throws CoreException |
260 |
*/ |
261 |
public IMarker[] getErrors() throws CoreException { |
262 |
if (errors == null) { |
263 |
errors = filterProblems(getAllProblems(), IMarker.SEVERITY_ERROR); |
264 |
} |
265 |
return errors; |
266 |
} |
267 |
|
268 |
/** |
269 |
* Returns all problem markers on the resource whose severity is <code>IMarker.SEVERITY_ERROR</code> |
270 |
* or <code>IMarker.SEVERITY_WARNING</code>. |
271 |
* @throws CoreException |
272 |
*/ |
273 |
public IMarker[] getWarningAndErrors() throws CoreException { |
274 |
if (errorsAndWarnings == null) { |
275 |
errorsAndWarnings = filterProblems(getAllProblems(), IMarker.SEVERITY_WARNING); |
276 |
} |
277 |
return errorsAndWarnings; |
278 |
} |
279 |
|
280 |
/** |
281 |
* Returns only those markers whose severity is equal to or greater than |
282 |
* the specified value. |
283 |
* |
284 |
* @param minimumSeverity the desired minimum severity |
285 |
* @return an array that contains only those markers whose severity is |
286 |
* greater than or equal to <code>minimumSeverity</code> |
287 |
* @throws CoreException |
288 |
*/ |
289 |
private static IMarker[] filterProblems(IMarker[] problems, int minimumSeverity) throws CoreException { |
290 |
List filteredProblems = new ArrayList(problems.length); |
291 |
for (int i = 0; i < problems.length; ++i) { |
292 |
Integer severity = (Integer) problems[i].getAttribute(IMarker.SEVERITY); |
293 |
if (severity != null) { |
294 |
if (severity.intValue() >= minimumSeverity) { |
295 |
filteredProblems.add(problems[i]); |
296 |
} |
297 |
} |
298 |
} |
299 |
return (IMarker[]) filteredProblems.toArray(new IMarker[filteredProblems.size()]); |
300 |
} |
301 |
|
302 |
} |
125 |
} |
303 |
} |