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.plugin.internal;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.descriptor.Parameter;
25  import org.apache.maven.shared.utils.logging.MessageBuilder;
26  import org.apache.maven.shared.utils.logging.MessageUtils;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.slf4j.Logger;
31  
32  /**
33   * Common implementations for plugin parameters configuration validation.
34   *
35   * @author Slawomir Jaranowski
36   */
37  abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator {
38  
39      // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
40      // so, we need ignore those
41  
42      // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
43      private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
44              "basedir",
45              "executedProject",
46              "localRepository",
47              "mojo",
48              "mojoExecution",
49              "plugin",
50              "project",
51              "reactorProjects",
52              "session",
53              "settings");
54  
55      private static final List<String> IGNORED_PROPERTY_PREFIX =
56              Arrays.asList("mojo.", "plugin.", "project.", "session.", "settings.");
57  
58      protected abstract Logger getLogger();
59  
60      protected static boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
61          if (config == null) {
62              return false;
63          }
64  
65          // there are sub items ... so configuration is declared
66          if (config.getChildCount() > 0) {
67              return true;
68          }
69  
70          String strValue = config.getValue();
71  
72          if (strValue == null || strValue.isEmpty()) {
73              return false;
74          }
75  
76          if (isIgnoredProperty(strValue)) {
77              return false;
78          }
79  
80          // for declaration like @Parameter( property = "config.property" )
81          // the value will contain ${config.property}
82  
83          try {
84              return expressionEvaluator.evaluate(strValue) != null;
85          } catch (ExpressionEvaluationException e) {
86              // not important
87              // will be reported during Mojo fields populate
88          }
89  
90          // fallback - in case of error in expressionEvaluator
91          return false;
92      }
93  
94      private static boolean isIgnoredProperty(String strValue) {
95          if (!strValue.startsWith("${")) {
96              return false;
97          }
98  
99          String propertyName = strValue.replace("${", "").replace("}", "");
100 
101         if (IGNORED_PROPERTY_VALUES.contains(propertyName)) {
102             return true;
103         }
104 
105         return IGNORED_PROPERTY_PREFIX.stream().anyMatch(propertyName::startsWith);
106     }
107 
108     protected abstract String getParameterLogReason(Parameter parameter);
109 
110     protected void logParameter(Parameter parameter) {
111         MessageBuilder messageBuilder = MessageUtils.buffer()
112                 .warning("Parameter '")
113                 .warning(parameter.getName())
114                 .warning('\'');
115 
116         if (parameter.getExpression() != null) {
117             String userProperty = parameter.getExpression().replace("${", "'").replace('}', '\'');
118             messageBuilder.warning(" (user property ").warning(userProperty).warning(")");
119         }
120 
121         messageBuilder.warning(" ").warning(getParameterLogReason(parameter));
122 
123         getLogger().warn(messageBuilder.toString());
124     }
125 }