1 | |
package org.apache.maven.plugin.pmd; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Arrays; |
26 | |
import java.util.Collection; |
27 | |
import java.util.Collections; |
28 | |
import java.util.HashSet; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.LinkedHashSet; |
31 | |
import java.util.List; |
32 | |
import java.util.Map; |
33 | |
import java.util.TreeMap; |
34 | |
|
35 | |
import net.sourceforge.pmd.PMD; |
36 | |
|
37 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
38 | |
import org.apache.maven.model.ReportPlugin; |
39 | |
import org.apache.maven.project.MavenProject; |
40 | |
import org.apache.maven.reporting.AbstractMavenReport; |
41 | |
import org.codehaus.plexus.util.FileUtils; |
42 | |
import org.codehaus.plexus.util.PathTool; |
43 | |
import org.codehaus.plexus.util.ReaderFactory; |
44 | |
import org.codehaus.plexus.util.StringUtils; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 24 | public abstract class AbstractPmdReport |
53 | |
extends AbstractMavenReport |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected File targetDirectory; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected File outputDirectory; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private Renderer siteRenderer; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected MavenProject project; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 24 | protected String format = "xml"; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private boolean linkXRef; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private File xrefLocation; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
private File xrefTestLocation; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private String[] excludes; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
private String[] includes; |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
private List compileSourceRoots; |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private List testSourceRoots; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private File[] excludeRoots; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected boolean includeTests; |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
protected boolean aggregate; |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
private String sourceEncoding; |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
private String outputEncoding; |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected List reactorProjects; |
206 | |
|
207 | |
|
208 | |
protected MavenProject getProject() |
209 | |
{ |
210 | 0 | return project; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
protected Renderer getSiteRenderer() |
215 | |
{ |
216 | 28 | return siteRenderer; |
217 | |
} |
218 | |
|
219 | |
protected String constructXRefLocation( boolean test ) |
220 | |
{ |
221 | 22 | String location = null; |
222 | 22 | if ( linkXRef ) |
223 | |
{ |
224 | 10 | File xrefLoc = test ? xrefTestLocation : xrefLocation; |
225 | |
|
226 | 10 | String relativePath = PathTool.getRelativePath( outputDirectory.getAbsolutePath(), xrefLoc.getAbsolutePath() ); |
227 | 10 | if ( StringUtils.isEmpty( relativePath ) ) |
228 | |
{ |
229 | 0 | relativePath = "."; |
230 | |
} |
231 | 10 | relativePath = relativePath + "/" + xrefLoc.getName(); |
232 | 10 | if ( xrefLoc.exists() ) |
233 | |
{ |
234 | |
|
235 | 4 | location = relativePath; |
236 | |
} |
237 | |
else |
238 | |
{ |
239 | |
|
240 | 6 | for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); ) |
241 | |
{ |
242 | 6 | ReportPlugin plugin = (ReportPlugin) reports.next(); |
243 | |
|
244 | 6 | String artifactId = plugin.getArtifactId(); |
245 | 6 | if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) |
246 | |
{ |
247 | 6 | location = relativePath; |
248 | |
} |
249 | 6 | } |
250 | |
} |
251 | |
|
252 | 10 | if ( location == null ) |
253 | |
{ |
254 | 0 | getLog().warn( "Unable to locate Source XRef to link to - DISABLED" ); |
255 | |
} |
256 | |
} |
257 | 22 | return location; |
258 | |
} |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
protected Map getFilesToProcess() |
267 | |
throws IOException |
268 | |
{ |
269 | 22 | String sourceXref = constructXRefLocation( false ); |
270 | 22 | String testXref = includeTests ? constructXRefLocation( true ) : ""; |
271 | |
|
272 | 22 | if ( aggregate && !project.isExecutionRoot() ) |
273 | |
{ |
274 | 0 | return Collections.EMPTY_MAP; |
275 | |
} |
276 | |
|
277 | 22 | if ( excludeRoots == null ) |
278 | |
{ |
279 | 18 | excludeRoots = new File[0]; |
280 | |
} |
281 | 22 | Collection excludeRootFiles = new HashSet( excludeRoots.length ); |
282 | |
|
283 | 22 | for ( int i = 0; i < excludeRoots.length; i++ ) |
284 | |
{ |
285 | 0 | File file = excludeRoots[i]; |
286 | 0 | if ( file.isDirectory() ) |
287 | |
{ |
288 | 0 | excludeRootFiles.add( file ); |
289 | |
} |
290 | |
} |
291 | |
|
292 | 22 | List directories = new ArrayList(); |
293 | |
|
294 | 22 | for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); ) |
295 | |
{ |
296 | 18 | String root = (String) i.next(); |
297 | 18 | File sroot = new File( root ); |
298 | 18 | directories.add( new PmdFileInfo( project, sroot, sourceXref ) ); |
299 | 18 | } |
300 | |
|
301 | 18 | if ( includeTests ) |
302 | |
{ |
303 | 0 | for ( Iterator i = testSourceRoots.iterator(); i.hasNext(); ) |
304 | |
{ |
305 | 0 | String root = (String) i.next(); |
306 | 0 | File sroot = new File( root ); |
307 | 0 | directories.add( new PmdFileInfo( project, sroot, testXref ) ); |
308 | 0 | } |
309 | |
} |
310 | 18 | if ( aggregate ) |
311 | |
{ |
312 | 0 | for ( Iterator i = reactorProjects.iterator(); i.hasNext(); ) |
313 | |
{ |
314 | 0 | MavenProject localProject = (MavenProject) i.next(); |
315 | 0 | for ( Iterator i2 = localProject.getCompileSourceRoots().iterator(); i2.hasNext(); ) |
316 | |
{ |
317 | 0 | String root = (String) i2.next(); |
318 | 0 | File sroot = new File( root ); |
319 | 0 | directories.add( new PmdFileInfo( localProject, sroot, sourceXref ) ); |
320 | 0 | } |
321 | 0 | if ( includeTests ) |
322 | |
{ |
323 | 0 | for ( Iterator i2 = localProject.getTestCompileSourceRoots().iterator(); i2.hasNext(); ) |
324 | |
{ |
325 | 0 | String root = (String) i2.next(); |
326 | 0 | File sroot = new File( root ); |
327 | 0 | directories.add( new PmdFileInfo( localProject, sroot, testXref ) ); |
328 | 0 | } |
329 | |
} |
330 | 0 | } |
331 | |
|
332 | |
} |
333 | |
|
334 | 18 | String excluding = getExcludes(); |
335 | 18 | getLog().debug( "Exclusions: " + excluding ); |
336 | 18 | String including = getIncludes(); |
337 | 18 | getLog().debug( "Inclusions: " + including ); |
338 | |
|
339 | 18 | Map files = new TreeMap(); |
340 | |
|
341 | 18 | for ( Iterator it = directories.iterator(); it.hasNext(); ) |
342 | |
{ |
343 | 18 | PmdFileInfo finfo = (PmdFileInfo) it.next(); |
344 | 18 | File sourceDirectory = finfo.getSourceDirectory(); |
345 | 18 | if ( sourceDirectory.isDirectory() && !excludeRootFiles.contains( sourceDirectory ) ) |
346 | |
{ |
347 | 18 | List newfiles = FileUtils.getFiles( sourceDirectory, including, excluding ); |
348 | 18 | for ( Iterator it2 = newfiles.iterator(); it2.hasNext(); ) |
349 | |
{ |
350 | 50 | files.put( it2.next(), finfo ); |
351 | |
} |
352 | |
} |
353 | 18 | } |
354 | |
|
355 | 18 | return files; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
private String getIncludes() |
364 | |
{ |
365 | 18 | Collection patterns = new LinkedHashSet(); |
366 | 18 | if ( includes != null ) |
367 | |
{ |
368 | 0 | patterns.addAll( Arrays.asList( includes ) ); |
369 | |
} |
370 | 18 | if ( patterns.isEmpty() ) |
371 | |
{ |
372 | 18 | patterns.add( "**/*.java" ); |
373 | |
} |
374 | 18 | return StringUtils.join( patterns.iterator(), "," ); |
375 | |
} |
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
private String getExcludes() |
383 | |
{ |
384 | 18 | Collection patterns = new LinkedHashSet( FileUtils.getDefaultExcludesAsList() ); |
385 | 18 | if ( excludes != null ) |
386 | |
{ |
387 | 8 | patterns.addAll( Arrays.asList( excludes ) ); |
388 | |
} |
389 | 18 | return StringUtils.join( patterns.iterator(), "," ); |
390 | |
} |
391 | |
|
392 | |
protected boolean isHtml() |
393 | |
{ |
394 | 14 | return "html".equals( format ); |
395 | |
} |
396 | |
|
397 | |
|
398 | |
public boolean canGenerateReport() |
399 | |
{ |
400 | 20 | if ( aggregate && !project.isExecutionRoot() ) |
401 | |
{ |
402 | 0 | return false; |
403 | |
} |
404 | |
|
405 | 20 | if ( "pom".equals( project.getPackaging() ) && !aggregate ) |
406 | |
{ |
407 | 0 | return false; |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | 20 | if ( "xml".equals( format ) ) |
413 | |
{ |
414 | 12 | return true; |
415 | |
} |
416 | |
try |
417 | |
{ |
418 | 8 | Map filesToProcess = getFilesToProcess(); |
419 | 4 | if ( filesToProcess.isEmpty() ) |
420 | |
{ |
421 | 0 | return false; |
422 | |
} |
423 | |
} |
424 | 0 | catch ( IOException e ) |
425 | |
{ |
426 | 0 | getLog().error( e ); |
427 | 4 | } |
428 | 4 | return true; |
429 | |
} |
430 | |
|
431 | |
|
432 | |
protected String getOutputDirectory() |
433 | |
{ |
434 | 54 | return outputDirectory.getAbsolutePath(); |
435 | |
} |
436 | |
|
437 | |
protected String getSourceEncoding() |
438 | |
{ |
439 | 166 | return sourceEncoding; |
440 | |
} |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
protected String getOutputEncoding() |
449 | |
{ |
450 | 28 | return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8; |
451 | |
} |
452 | |
|
453 | |
static String getPmdVersion() |
454 | |
{ |
455 | |
try |
456 | |
{ |
457 | 16 | return (String) PMD.class.getField( "VERSION" ).get( null ); |
458 | |
} |
459 | 0 | catch ( IllegalAccessException e ) |
460 | |
{ |
461 | 0 | throw new RuntimeException( "PMD VERSION field not accessible", e ); |
462 | |
} |
463 | 0 | catch ( NoSuchFieldException e ) |
464 | |
{ |
465 | 0 | throw new RuntimeException( "PMD VERSION field not found", e ); |
466 | |
} |
467 | |
} |
468 | |
} |