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.api.services.MessageBuilderFactory;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.PluginValidationManager;
28  import org.apache.maven.plugin.descriptor.MojoDescriptor;
29  import org.apache.maven.plugin.descriptor.Parameter;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
31  import org.codehaus.plexus.configuration.PlexusConfiguration;
32  
33  /**
34   * Print warnings if deprecated mojo or parameters of plugin are used in configuration.
35   *
36   */
37  @Singleton
38  @Named
39  class DeprecatedPluginValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
40  
41      private final MessageBuilderFactory messageBuilderFactory;
42  
43      @Inject
44      DeprecatedPluginValidator(
45              PluginValidationManager pluginValidationManager, MessageBuilderFactory messageBuilderFactory) {
46          super(pluginValidationManager);
47          this.messageBuilderFactory = messageBuilderFactory;
48      }
49  
50      @Override
51      protected String getParameterLogReason(Parameter parameter) {
52          return "is deprecated: " + parameter.getDeprecated();
53      }
54  
55      @Override
56      protected void doValidate(
57              MavenSession mavenSession,
58              MojoDescriptor mojoDescriptor,
59              Class<?> mojoClass,
60              PlexusConfiguration pomConfiguration,
61              ExpressionEvaluator expressionEvaluator) {
62          if (mojoDescriptor.getDeprecated() != null) {
63              pluginValidationManager.reportPluginMojoValidationIssue(
64                      PluginValidationManager.IssueLocality.INTERNAL,
65                      mavenSession,
66                      mojoDescriptor,
67                      mojoClass,
68                      logDeprecatedMojo(mojoDescriptor));
69          }
70  
71          if (mojoDescriptor.getParameters() != null) {
72              mojoDescriptor.getParameters().stream()
73                      .filter(parameter -> parameter.getDeprecated() != null)
74                      .filter(Parameter::isEditable)
75                      .forEach(parameter -> checkParameter(
76                              mavenSession, mojoDescriptor, mojoClass, parameter, pomConfiguration, expressionEvaluator));
77          }
78      }
79  
80      private void checkParameter(
81              MavenSession mavenSession,
82              MojoDescriptor mojoDescriptor,
83              Class<?> mojoClass,
84              Parameter parameter,
85              PlexusConfiguration pomConfiguration,
86              ExpressionEvaluator expressionEvaluator) {
87          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
88  
89          if (isValueSet(config, expressionEvaluator)) {
90              pluginValidationManager.reportPluginMojoValidationIssue(
91                      PluginValidationManager.IssueLocality.INTERNAL,
92                      mavenSession,
93                      mojoDescriptor,
94                      mojoClass,
95                      formatParameter(parameter));
96          }
97      }
98  
99      private String logDeprecatedMojo(MojoDescriptor mojoDescriptor) {
100         return messageBuilderFactory
101                 .builder()
102                 .warning("Goal '")
103                 .warning(mojoDescriptor.getGoal())
104                 .warning("' is deprecated: ")
105                 .warning(mojoDescriptor.getDeprecated())
106                 .toString();
107     }
108 }