View Javadoc

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.EnforcerRule;
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
24  
25  /**
26   * This rule checks that certain environment variable is set.
27   *
28   * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a>
29   */
30  public class RequireEnvironmentVariable
31      extends AbstractPropertyEnforcerRule
32  {
33  
34      /**
35       * Specify the required variable.
36       */
37      public String variableName = null;
38  
39      /**
40       * @param variableName the variable name
41       * 
42       * @deprecated the visibility will be reduced to private with the next major version
43       * @see {@link #setVariableName(String)}
44       * @see {@link #getVariableName()}
45       */
46      public final void setVariableName( String variableName )
47      {
48          this.variableName = variableName;
49      }
50      
51      public final String getVariableName()
52      {
53          return variableName;
54      }
55  
56      @Override
57      public String resolveValue( EnforcerRuleHelper helper )
58      {
59          String envValue = System.getenv( variableName );
60          return envValue;
61      }
62  
63      public boolean isCacheable()
64      {
65          // environment variables won't change while maven is on the run
66          return true;
67      }
68  
69      public boolean isResultValid( EnforcerRule cachedRule )
70      {
71          // this rule shall always have the same result, since environment
72          // variables are set before maven is launched
73          return true;
74      }
75  
76      public String getCacheId()
77      {
78          return variableName;
79      }
80  
81      @Override
82      public String getPropertyName()
83      {
84          return variableName;
85      }
86  
87      @Override
88      public String getName()
89      {
90          return "Environment variable";
91      }
92  }