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      private String variableName = null;
38  
39      /**
40       * @param variableName the variable name
41       * 
42       * @see {@link #setVariableName(String)}
43       * @see {@link #getVariableName()}
44       */
45      public final void setVariableName( String variableName )
46      {
47          this.variableName = variableName;
48      }
49      
50      public final String getVariableName()
51      {
52          return variableName;
53      }
54  
55      @Override
56      public String resolveValue( EnforcerRuleHelper helper )
57      {
58          String envValue = System.getenv( variableName );
59          return envValue;
60      }
61  
62      public boolean isCacheable()
63      {
64          // environment variables won't change while maven is on the run
65          return true;
66      }
67  
68      public boolean isResultValid( EnforcerRule cachedRule )
69      {
70          // this rule shall always have the same result, since environment
71          // variables are set before maven is launched
72          return true;
73      }
74  
75      public String getCacheId()
76      {
77          return variableName;
78      }
79  
80      @Override
81      public String getPropertyName()
82      {
83          return variableName;
84      }
85  
86      @Override
87      public String getName()
88      {
89          return "Environment variable";
90      }
91  }