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 org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
24 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
25
26
27
28
29
30
31 public class RequireProperty
32 extends AbstractNonCacheableEnforcerRule
33 {
34
35
36 public String property = null;
37
38
39 public String regex = null;
40
41
42 public String regexMessage = null;
43
44
45
46
47
48
49
50 public void execute( EnforcerRuleHelper helper )
51 throws EnforcerRuleException
52 {
53 Object propValue = null;
54 try
55 {
56 propValue = helper.evaluate( "${" + property + "}" );
57 }
58 catch ( ExpressionEvaluationException eee )
59 {
60 throw new EnforcerRuleException( "Unable to evaluate property: " + property, eee );
61 }
62
63
64 if ( propValue == null )
65 {
66 if ( message == null )
67 {
68 message = "Property \"" + property + "\" is required for this build.";
69 }
70 throw new EnforcerRuleException( message );
71 }
72
73 if ( regex != null && !propValue.toString().matches( regex ) )
74 {
75 if ( regexMessage == null )
76 {
77 regexMessage =
78 "Property \"" + property + "\" evaluates to \"" + propValue + "\". " +
79 "This does not match the regular expression \"" + regex + "\"";
80 }
81 throw new EnforcerRuleException( regexMessage );
82 }
83 }
84 }