Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RequireProperty |
|
| 10.0;10 |
1 | package org.apache.maven.plugins.enforcer; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * This rule checks that certain properties are set. | |
28 | * | |
29 | * @author Paul Gier | |
30 | */ | |
31 | 0 | public class RequireProperty |
32 | extends AbstractNonCacheableEnforcerRule | |
33 | { | |
34 | ||
35 | /** Specify the required property. */ | |
36 | 0 | public String property = null; |
37 | ||
38 | /** Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). */ | |
39 | 0 | public String regex = null; |
40 | ||
41 | /** Specify a warning message if the regular expression is not matched. */ | |
42 | 0 | public String regexMessage = null; |
43 | ||
44 | /** | |
45 | * Execute the rule. | |
46 | * | |
47 | * @param helper the helper | |
48 | * @throws EnforcerRuleException the enforcer rule exception | |
49 | */ | |
50 | public void execute( EnforcerRuleHelper helper ) | |
51 | throws EnforcerRuleException | |
52 | { | |
53 | 0 | Object propValue = null; |
54 | try | |
55 | { | |
56 | 0 | propValue = helper.evaluate( "${" + property + "}" ); |
57 | } | |
58 | 0 | catch ( ExpressionEvaluationException eee ) |
59 | { | |
60 | 0 | throw new EnforcerRuleException( "Unable to evaluate property: " + property, eee ); |
61 | 0 | } |
62 | ||
63 | // Check that the property is not null or empty string | |
64 | 0 | if ( propValue == null ) |
65 | { | |
66 | 0 | if ( message == null ) |
67 | { | |
68 | 0 | message = "Property \"" + property + "\" is required for this build."; |
69 | } | |
70 | 0 | throw new EnforcerRuleException( message ); |
71 | } | |
72 | // If there is a regex, check that the property matches it | |
73 | 0 | if ( regex != null && !propValue.toString().matches( regex ) ) |
74 | { | |
75 | 0 | if ( regexMessage == null ) |
76 | { | |
77 | 0 | regexMessage = |
78 | "Property \"" + property + "\" evaluates to \"" + propValue + "\". " + | |
79 | "This does not match the regular expression \"" + regex + "\""; | |
80 | } | |
81 | 0 | throw new EnforcerRuleException( regexMessage ); |
82 | } | |
83 | 0 | } |
84 | } |