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   */
36  @Named
37  @Singleton
38  class ReadOnlyPluginParametersValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
39  
40      @Inject
41      ReadOnlyPluginParametersValidator(PluginValidationManager pluginValidationManager) {
42          super(pluginValidationManager);
43      }
44  
45      @Override
46      protected String getParameterLogReason(Parameter parameter) {
47          return "is read-only, must not be used in configuration";
48      }
49  
50      @Override
51      protected void doValidate(
52              MavenSession mavenSession,
53              MojoDescriptor mojoDescriptor,
54              Class<?> mojoClass,
55              PlexusConfiguration pomConfiguration,
56              ExpressionEvaluator expressionEvaluator) {
57          if (mojoDescriptor.getParameters() == null) {
58              return;
59          }
60  
61          mojoDescriptor.getParameters().stream()
62                  .filter(parameter -> !parameter.isEditable())
63                  .forEach(parameter -> checkParameter(
64                          mavenSession, mojoDescriptor, mojoClass, parameter, pomConfiguration, expressionEvaluator));
65      }
66  
67      private void checkParameter(
68              MavenSession mavenSession,
69              MojoDescriptor mojoDescriptor,
70              Class<?> mojoClass,
71              Parameter parameter,
72              PlexusConfiguration pomConfiguration,
73              ExpressionEvaluator expressionEvaluator) {
74          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
75  
76          if (isValueSet(config, expressionEvaluator)) {
77              pluginValidationManager.reportPluginMojoValidationIssue(
78                      PluginValidationManager.IssueLocality.INTERNAL,
79                      mavenSession,
80                      mojoDescriptor,
81                      mojoClass,
82                      formatParameter(parameter));
83          }
84      }
85  }