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