View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.property;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.Objects;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.enforcer.rules.utils.ExpressionEvaluator;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
29  
30  /**
31   * This rule checks that certain properties are set.
32   *
33   * @author Paul Gier
34   */
35  @Named("requireProperty")
36  public final class RequireProperty extends AbstractPropertyEnforcerRule {
37  
38      /**
39       * Specify the required property.
40       */
41      private String property = null;
42  
43      private final ExpressionEvaluator evaluator;
44  
45      @Inject
46      public RequireProperty(ExpressionEvaluator evaluator) {
47          this.evaluator = Objects.requireNonNull(evaluator);
48      }
49  
50      public void setProperty(String property) {
51          this.property = property;
52      }
53  
54      @Override
55      public Object resolveValue() throws EnforcerRuleException {
56          try {
57              return evaluator.evaluate("${" + property + "}");
58          } catch (ExpressionEvaluationException e) {
59              throw new EnforcerRuleException(e);
60          }
61      }
62  
63      @Override
64      public String getPropertyName() {
65          return property;
66      }
67  
68      @Override
69      public String getName() {
70          return "Property";
71      }
72  
73      @Override
74      public String toString() {
75          return String.format(
76                  "RequireProperty[message=%s, property=%s, regex=%s, regexMessage=%s]",
77                  getMessage(), property, getRegex(), getRegexMessage());
78      }
79  }