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.List;
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 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 protected boolean fail = true;
88
89
90
91
92
93
94
95 protected boolean skip = false;
96
97
98
99
100
101
102
103 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 protected boolean ignoreCache = false;
122
123
124
125
126 protected static Hashtable<String, EnforcerRule> cache = new Hashtable<String, EnforcerRule>();
127
128
129
130
131 protected PlexusContainer container;
132
133 public void contextualize ( Context context )
134 throws ContextException
135 {
136 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
137 }
138
139
140
141
142 public void execute ()
143 throws MojoExecutionException
144 {
145 Log log = this.getLog();
146
147 EnforcerExpressionEvaluator evaluator = new EnforcerExpressionEvaluator( session, translator, project );
148
149
150 if ( !skip )
151 {
152
153 List<String> list = new ArrayList<String>();
154
155
156 if ( rules != null && rules.length > 0 )
157 {
158 String currentRule = "Unknown";
159
160
161 EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper( session, evaluator, log, container );
162
163
164
165 if ( !fail )
166 {
167 failFast = false;
168 }
169
170
171 for ( int i = 0; i < rules.length; i++ )
172 {
173
174
175 EnforcerRule rule = rules[i];
176 if ( rule != null )
177 {
178
179
180 currentRule = rule.getClass().getName();
181 log.debug( "Executing rule: " + currentRule );
182 try
183 {
184 if ( ignoreCache || shouldExecute( rule ) )
185 {
186
187
188 synchronized ( rule )
189 {
190 rule.execute( helper );
191 }
192 }
193 }
194 catch ( EnforcerRuleException e )
195 {
196
197
198
199 if ( failFast )
200 {
201 throw new MojoExecutionException( currentRule + " failed with message:\n"
202 + e.getMessage(), e );
203 }
204 else
205 {
206 list.add( "Rule " + i + ": " + currentRule + " failed with message:\n" + e.getMessage() );
207 log.debug( "Adding failure due to exception", e );
208 }
209 }
210 }
211 }
212
213
214 if ( !list.isEmpty() )
215 {
216 for ( String failure : list )
217 {
218 log.warn( failure );
219 }
220 if ( fail )
221 {
222 throw new MojoExecutionException(
223 "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." );
224 }
225 }
226 }
227 else
228 {
229 throw new MojoExecutionException(
230 "No rules are configured. Use the skip flag if you want to disable execution." );
231 }
232 }
233 else
234 {
235 log.info( "Skipping Rule Enforcement." );
236 }
237 }
238
239
240
241
242
243
244
245
246 protected boolean shouldExecute ( EnforcerRule rule )
247 {
248 if ( rule.isCacheable() )
249 {
250 Log log = this.getLog();
251 log.debug( "Rule " + rule.getClass().getName() + " is cacheable." );
252 String key = rule.getClass().getName() + " " + rule.getCacheId();
253 if ( EnforceMojo.cache.containsKey( key ) )
254 {
255 log.debug( "Key " + key + " was found in the cache" );
256 if ( rule.isResultValid( (EnforcerRule) cache.get( key ) ) )
257 {
258 log.debug( "The cached results are still valid. Skipping the rule: "+rule.getClass().getName() );
259 return false;
260 }
261 }
262
263
264 EnforceMojo.cache.put( key, rule );
265 }
266 return true;
267 }
268
269
270
271
272 public boolean isFail ()
273 {
274 return this.fail;
275 }
276
277
278
279
280 public void setFail ( boolean theFail )
281 {
282 this.fail = theFail;
283 }
284
285
286
287
288 public EnforcerRule[] getRules ()
289 {
290 return this.rules;
291 }
292
293
294
295
296 public void setRules ( EnforcerRule[] theRules )
297 {
298 this.rules = theRules;
299 }
300
301
302
303
304 public boolean isSkip ()
305 {
306 return this.skip;
307 }
308
309
310
311
312 public void setSkip ( boolean theSkip )
313 {
314 this.skip = theSkip;
315 }
316
317
318
319
320 public boolean isFailFast ()
321 {
322 return this.failFast;
323 }
324
325
326
327
328 public void setFailFast ( boolean theFailFast )
329 {
330 this.failFast = theFailFast;
331 }
332
333
334
335
336 public MavenProject getProject ()
337 {
338 return this.project;
339 }
340
341
342
343
344 public void setProject ( MavenProject theProject )
345 {
346 this.project = theProject;
347 }
348
349
350
351
352 public MavenSession getSession ()
353 {
354 return this.session;
355 }
356
357
358
359
360 public void setSession ( MavenSession theSession )
361 {
362 this.session = theSession;
363 }
364
365
366
367
368 public PathTranslator getTranslator ()
369 {
370 return this.translator;
371 }
372
373
374
375
376 public void setTranslator ( PathTranslator theTranslator )
377 {
378 this.translator = theTranslator;
379 }
380 }