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.io.Reader; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.maven.plugin.AbstractMojo; |
29 | |
import org.apache.maven.plugin.MojoExecutionException; |
30 | |
import org.apache.maven.plugin.MojoFailureException; |
31 | |
import org.apache.maven.project.MavenProject; |
32 | |
import org.codehaus.plexus.util.IOUtil; |
33 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 16 | public abstract class AbstractPmdViolationCheckMojo<D> |
42 | |
extends AbstractMojo |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
private File targetDirectory; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private boolean failOnViolation; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private String language; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
protected boolean aggregate; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private boolean verbose; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected MavenProject project; |
92 | |
|
93 | |
protected void executeCheck( String filename, String tagName, String key, int failurePriority ) |
94 | |
throws MojoFailureException, MojoExecutionException |
95 | |
{ |
96 | 8 | if ( aggregate && !project.isExecutionRoot() ) |
97 | |
{ |
98 | 0 | return; |
99 | |
} |
100 | |
|
101 | 8 | if ( "java".equals( language ) || aggregate ) |
102 | |
{ |
103 | 8 | File outputFile = new File( targetDirectory, filename ); |
104 | |
|
105 | 8 | if ( outputFile.exists() ) |
106 | |
{ |
107 | 8 | Reader reader = null; |
108 | |
try |
109 | |
{ |
110 | 8 | ViolationDetails<D> violations = getViolations( outputFile, failurePriority ); |
111 | |
|
112 | 8 | List<D> failures = violations.getFailureDetails(); |
113 | 8 | List<D> warnings = violations.getWarningDetails(); |
114 | |
|
115 | 8 | if ( verbose ) |
116 | |
{ |
117 | 2 | printErrors( failures, warnings ); |
118 | |
} |
119 | |
|
120 | 8 | int failureCount = failures.size(); |
121 | 8 | int warningCount = warnings.size(); |
122 | |
|
123 | 8 | String message = getMessage( failureCount, warningCount, key, outputFile ); |
124 | |
|
125 | 8 | if ( failureCount > 0 && failOnViolation ) |
126 | |
{ |
127 | 2 | throw new MojoFailureException( message ); |
128 | |
} |
129 | |
|
130 | 6 | this.getLog().info( message ); |
131 | |
} |
132 | 0 | catch ( IOException e ) |
133 | |
{ |
134 | 0 | throw new MojoExecutionException( |
135 | |
"Unable to read PMD results xml: " + outputFile.getAbsolutePath(), |
136 | |
e ); |
137 | |
} |
138 | 0 | catch ( XmlPullParserException e ) |
139 | |
{ |
140 | 0 | throw new MojoExecutionException( |
141 | |
"Unable to read PMD results xml: " + outputFile.getAbsolutePath(), |
142 | |
e ); |
143 | |
} |
144 | |
finally |
145 | |
{ |
146 | 8 | IOUtil.close( reader ); |
147 | 6 | } |
148 | 6 | } |
149 | |
else |
150 | |
{ |
151 | 0 | throw new MojoFailureException( "Unable to perform check, " + "unable to find " + outputFile ); |
152 | |
} |
153 | |
} |
154 | 6 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private ViolationDetails<D> getViolations( File analysisFile, int failurePriority ) |
168 | |
throws XmlPullParserException, IOException |
169 | |
{ |
170 | 8 | List<D> failures = new ArrayList<D>(); |
171 | 8 | List<D> warnings = new ArrayList<D>(); |
172 | |
|
173 | 8 | List<D> violations = getErrorDetails( analysisFile ); |
174 | |
|
175 | 8 | for( D violation : violations ) |
176 | |
{ |
177 | 38 | int priority = getPriority( violation ); |
178 | 38 | if ( priority <= failurePriority ) |
179 | |
{ |
180 | 12 | failures.add( violation ); |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 26 | warnings.add( violation ); |
185 | |
} |
186 | 38 | } |
187 | |
|
188 | 8 | ViolationDetails<D> details = newViolationDetailsInstance(); |
189 | 8 | details.setFailureDetails( failures ); |
190 | 8 | details.setWarningDetails( warnings ); |
191 | 8 | return details; |
192 | |
} |
193 | |
|
194 | |
protected abstract int getPriority( D errorDetail ); |
195 | |
|
196 | |
protected abstract ViolationDetails<D> newViolationDetailsInstance(); |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
protected void printErrors( List<D> failures, List<D> warnings ) |
207 | |
{ |
208 | 2 | for ( D warning : warnings ) |
209 | |
{ |
210 | 2 | printError( warning, "Warning" ); |
211 | |
} |
212 | |
|
213 | 2 | for ( D failure : failures ) |
214 | |
{ |
215 | 10 | printError( failure, "Failure" ); |
216 | |
} |
217 | 2 | } |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
private String getMessage( int failureCount, int warningCount, String key, File outputFile ) |
229 | |
{ |
230 | 8 | StringBuffer message = new StringBuffer(); |
231 | 8 | if ( failureCount > 0 || warningCount > 0 ) |
232 | |
{ |
233 | 8 | if ( failureCount > 0 ) |
234 | |
{ |
235 | 4 | message.append( "You have " + failureCount + " " + key + ( failureCount > 1 ? "s" : "" ) ); |
236 | |
} |
237 | |
|
238 | 8 | if ( warningCount > 0 ) |
239 | |
{ |
240 | 6 | if ( failureCount > 0 ) |
241 | |
{ |
242 | 2 | message.append( " and " ); |
243 | |
} |
244 | |
else |
245 | |
{ |
246 | 4 | message.append( "You have " ); |
247 | |
} |
248 | 6 | message.append( warningCount + " warning" + ( warningCount > 1 ? "s" : "" ) ); |
249 | |
} |
250 | |
|
251 | 8 | message.append( ". For more details see:" ).append( outputFile.getAbsolutePath() ); |
252 | |
} |
253 | 8 | return message.toString(); |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
protected abstract void printError( D item, String severity ); |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
protected abstract List<D> getErrorDetails( File analisysFile ) |
272 | |
throws XmlPullParserException, IOException; |
273 | |
} |