001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.enforcer.rule.api.EnforcerRule; 023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 024 025/** 026 * This rule checks that certain environment variable is set. 027 * 028 * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a> 029 */ 030public class RequireEnvironmentVariable 031 extends AbstractPropertyEnforcerRule 032{ 033 034 /** 035 * Specify the required variable. 036 */ 037 private String variableName = null; 038 039 /** 040 * @param variableName the variable name 041 * 042 * @see #setVariableName(String) 043 * @see #getVariableName() 044 */ 045 public final void setVariableName( String variableName ) 046 { 047 this.variableName = variableName; 048 } 049 050 public final String getVariableName() 051 { 052 return variableName; 053 } 054 055 @Override 056 public String resolveValue( EnforcerRuleHelper helper ) 057 { 058 String envValue = System.getenv( variableName ); 059 return envValue; 060 } 061 062 public boolean isCacheable() 063 { 064 // environment variables won't change while maven is on the run 065 return true; 066 } 067 068 public boolean isResultValid( EnforcerRule cachedRule ) 069 { 070 // this rule shall always have the same result, since environment 071 // variables are set before maven is launched 072 return true; 073 } 074 075 public String getCacheId() 076 { 077 return variableName; 078 } 079 080 @Override 081 public String getPropertyName() 082 { 083 return variableName; 084 } 085 086 @Override 087 public String getName() 088 { 089 return "Environment variable"; 090 } 091}