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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.plugin.PluginValidationManager;
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.Parameter;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
30  import org.codehaus.plexus.configuration.PlexusConfiguration;
31  
32  /**
33   * Print warnings if read-only parameters of a plugin are used in configuration.
34   *
35   * @author Slawomir Jaranowski
36   */
37  @Named
38  @Singleton
39  class ReadOnlyPluginParametersValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
40  
41      @Inject
42      ReadOnlyPluginParametersValidator(PluginValidationManager pluginValidationManager) {
43          super(pluginValidationManager);
44      }
45  
46      @Override
47      protected String getParameterLogReason(Parameter parameter) {
48          return "is read-only, must not be used in configuration";
49      }
50  
51      @Override
52      protected void doValidate(
53              MavenSession mavenSession,
54              MojoDescriptor mojoDescriptor,
55              Class<?> mojoClass,
56              PlexusConfiguration pomConfiguration,
57              ExpressionEvaluator expressionEvaluator) {
58          if (mojoDescriptor.getParameters() == null) {
59              return;
60          }
61  
62          mojoDescriptor.getParameters().stream()
63                  .filter(parameter -> !parameter.isEditable())
64                  .forEach(parameter -> checkParameter(
65                          mavenSession, mojoDescriptor, mojoClass, parameter, pomConfiguration, expressionEvaluator));
66      }
67  
68      private void checkParameter(
69              MavenSession mavenSession,
70              MojoDescriptor mojoDescriptor,
71              Class<?> mojoClass,
72              Parameter parameter,
73              PlexusConfiguration pomConfiguration,
74              ExpressionEvaluator expressionEvaluator) {
75          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
76  
77          if (isValueSet(config, expressionEvaluator)) {
78              pluginValidationManager.reportPluginMojoValidationIssue(
79                      PluginValidationManager.IssueLocality.INTERNAL,
80                      mavenSession,
81                      mojoDescriptor,
82                      mojoClass,
83                      formatParameter(parameter));
84          }
85      }
86  }