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 #setVariableName(String)
43 * @see #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 @Override
63 public boolean isCacheable()
64 {
65 // environment variables won't change while maven is on the run
66 return true;
67 }
68
69 @Override
70 public boolean isResultValid( EnforcerRule cachedRule )
71 {
72 // this rule shall always have the same result, since environment
73 // variables are set before maven is launched
74 return true;
75 }
76
77 @Override
78 public String getCacheId()
79 {
80 return variableName;
81 }
82
83 @Override
84 public String getPropertyName()
85 {
86 return variableName;
87 }
88
89 @Override
90 public String getName()
91 {
92 return "Environment variable";
93 }
94 }