1 | |
package org.apache.maven.plugins.enforcer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Hashtable; |
24 | |
import java.util.Iterator; |
25 | |
|
26 | |
import org.apache.maven.enforcer.rule.api.EnforcerRule; |
27 | |
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; |
28 | |
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; |
29 | |
import org.apache.maven.execution.MavenSession; |
30 | |
import org.apache.maven.plugin.AbstractMojo; |
31 | |
import org.apache.maven.plugin.MojoExecutionException; |
32 | |
import org.apache.maven.plugin.logging.Log; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.maven.project.path.PathTranslator; |
35 | |
import org.codehaus.plexus.PlexusConstants; |
36 | |
import org.codehaus.plexus.PlexusContainer; |
37 | |
import org.codehaus.plexus.context.Context; |
38 | |
import org.codehaus.plexus.context.ContextException; |
39 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 10 | public class EnforceMojo |
53 | |
extends AbstractMojo |
54 | |
implements Contextualizable |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
protected PathTranslator translator; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
protected MavenSession session; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
protected MavenProject project; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 10 | protected boolean fail = true; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | 10 | protected boolean skip = false; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | 10 | protected boolean failFast = false; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private EnforcerRule[] rules; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 10 | protected boolean ignoreCache = false; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 2 | protected static Hashtable cache = new Hashtable(); |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected PlexusContainer container; |
132 | |
|
133 | |
public void contextualize ( Context context ) |
134 | |
throws ContextException |
135 | |
{ |
136 | 0 | container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void execute () |
143 | |
throws MojoExecutionException |
144 | |
{ |
145 | 26 | Log log = this.getLog(); |
146 | |
|
147 | 26 | EnforcerExpressionEvaluator evaluator = new EnforcerExpressionEvaluator( session, translator, project ); |
148 | |
|
149 | |
|
150 | 26 | if ( !skip ) |
151 | |
{ |
152 | |
|
153 | 26 | ArrayList list = new ArrayList(); |
154 | |
|
155 | |
|
156 | 26 | if ( rules != null && rules.length > 0 ) |
157 | |
{ |
158 | 24 | String currentRule = "Unknown"; |
159 | |
|
160 | |
|
161 | 24 | EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper( session, evaluator, log, container ); |
162 | |
|
163 | |
|
164 | |
|
165 | 24 | if ( !fail ) |
166 | |
{ |
167 | 2 | failFast = false; |
168 | |
} |
169 | |
|
170 | |
|
171 | 244 | for ( int i = 0; i < rules.length; i++ ) |
172 | |
{ |
173 | |
|
174 | |
|
175 | 222 | EnforcerRule rule = rules[i]; |
176 | 222 | if ( rule != null ) |
177 | |
{ |
178 | |
|
179 | |
|
180 | 48 | currentRule = rule.getClass().getName(); |
181 | 48 | log.debug( "Executing rule: " + currentRule ); |
182 | |
try |
183 | |
{ |
184 | 48 | if ( ignoreCache || shouldExecute( rule ) ) |
185 | |
{ |
186 | |
|
187 | |
|
188 | 34 | synchronized ( rule ) |
189 | |
{ |
190 | 34 | rule.execute( helper ); |
191 | 24 | } |
192 | |
} |
193 | |
} |
194 | 10 | catch ( EnforcerRuleException e ) |
195 | |
{ |
196 | |
|
197 | |
|
198 | |
|
199 | 10 | if ( failFast ) |
200 | |
{ |
201 | 2 | throw new MojoExecutionException( currentRule + " failed with message:\n" |
202 | |
+ e.getMessage(), e ); |
203 | |
} |
204 | |
else |
205 | |
{ |
206 | 8 | list.add( "Rule " + i + ": " + currentRule + " failed with message:\n" + e.getMessage() ); |
207 | 8 | log.debug( "Adding failure due to exception", e ); |
208 | |
} |
209 | 38 | } |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
|
214 | 22 | if ( !list.isEmpty() ) |
215 | |
{ |
216 | 4 | Iterator iter = list.iterator(); |
217 | 12 | while ( iter.hasNext() ) |
218 | |
{ |
219 | 8 | String failure = (String) iter.next(); |
220 | 8 | log.warn( failure ); |
221 | 8 | } |
222 | 4 | if ( fail ) |
223 | |
{ |
224 | 2 | throw new MojoExecutionException( |
225 | |
"Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." ); |
226 | |
} |
227 | |
} |
228 | 20 | } |
229 | |
else |
230 | |
{ |
231 | 2 | throw new MojoExecutionException( |
232 | |
"No rules are configured. Use the skip flag if you want to disable execution." ); |
233 | |
} |
234 | 20 | } |
235 | |
else |
236 | |
{ |
237 | 0 | log.info( "Skipping Rule Enforcement." ); |
238 | |
} |
239 | 20 | } |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
protected boolean shouldExecute ( EnforcerRule rule ) |
249 | |
{ |
250 | 44 | if ( rule.isCacheable() ) |
251 | |
{ |
252 | 28 | Log log = this.getLog(); |
253 | 28 | log.debug( "Rule " + rule.getClass().getName() + " is cacheable." ); |
254 | 28 | String key = rule.getClass().getName() + " " + rule.getCacheId(); |
255 | 28 | if ( EnforceMojo.cache.containsKey( key ) ) |
256 | |
{ |
257 | 16 | log.debug( "Key " + key + " was found in the cache" ); |
258 | 16 | if ( rule.isResultValid( (EnforcerRule) cache.get( key ) ) ) |
259 | |
{ |
260 | 14 | log.debug( "The cached results are still valid. Skipping the rule: "+rule.getClass().getName() ); |
261 | 14 | return false; |
262 | |
} |
263 | |
} |
264 | |
|
265 | |
|
266 | 14 | EnforceMojo.cache.put( key, rule ); |
267 | |
} |
268 | 30 | return true; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public boolean isFail () |
275 | |
{ |
276 | 0 | return this.fail; |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public void setFail ( boolean theFail ) |
283 | |
{ |
284 | 14 | this.fail = theFail; |
285 | 14 | } |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
public EnforcerRule[] getRules () |
291 | |
{ |
292 | 0 | return this.rules; |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public void setRules ( EnforcerRule[] theRules ) |
299 | |
{ |
300 | 18 | this.rules = theRules; |
301 | 18 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public boolean isSkip () |
307 | |
{ |
308 | 0 | return this.skip; |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
public void setSkip ( boolean theSkip ) |
315 | |
{ |
316 | 0 | this.skip = theSkip; |
317 | 0 | } |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public boolean isFailFast () |
323 | |
{ |
324 | 0 | return this.failFast; |
325 | |
} |
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public void setFailFast ( boolean theFailFast ) |
331 | |
{ |
332 | 4 | this.failFast = theFailFast; |
333 | 4 | } |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
public MavenProject getProject () |
339 | |
{ |
340 | 0 | return this.project; |
341 | |
} |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
public void setProject ( MavenProject theProject ) |
347 | |
{ |
348 | 10 | this.project = theProject; |
349 | 10 | } |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
public MavenSession getSession () |
355 | |
{ |
356 | 0 | return this.session; |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public void setSession ( MavenSession theSession ) |
363 | |
{ |
364 | 10 | this.session = theSession; |
365 | 10 | } |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
public PathTranslator getTranslator () |
371 | |
{ |
372 | 0 | return this.translator; |
373 | |
} |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
public void setTranslator ( PathTranslator theTranslator ) |
379 | |
{ |
380 | 0 | this.translator = theTranslator; |
381 | 0 | } |
382 | |
} |