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.apache.maven.shared.utils.logging.MessageUtils;
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   * @author Slawomir Jaranowski
37   */
38  @Singleton
39  @Named
40  class DeprecatedPluginValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
41  
42      @Inject
43      DeprecatedPluginValidator(PluginValidationManager pluginValidationManager) {
44          super(pluginValidationManager);
45      }
46  
47      @Override
48      protected String getParameterLogReason(Parameter parameter) {
49          return "is deprecated: " + parameter.getDeprecated();
50      }
51  
52      @Override
53      protected void doValidate(
54              MavenSession mavenSession,
55              MojoDescriptor mojoDescriptor,
56              Class<?> mojoClass,
57              PlexusConfiguration pomConfiguration,
58              ExpressionEvaluator expressionEvaluator) {
59          if (mojoDescriptor.getDeprecated() != null) {
60              pluginValidationManager.reportPluginMojoValidationIssue(
61                      PluginValidationManager.IssueLocality.INTERNAL,
62                      mavenSession,
63                      mojoDescriptor,
64                      mojoClass,
65                      logDeprecatedMojo(mojoDescriptor));
66          }
67  
68          if (mojoDescriptor.getParameters() != null) {
69              mojoDescriptor.getParameters().stream()
70                      .filter(parameter -> parameter.getDeprecated() != null)
71                      .filter(Parameter::isEditable)
72                      .forEach(parameter -> checkParameter(
73                              mavenSession, mojoDescriptor, mojoClass, parameter, pomConfiguration, expressionEvaluator));
74          }
75      }
76  
77      private void checkParameter(
78              MavenSession mavenSession,
79              MojoDescriptor mojoDescriptor,
80              Class<?> mojoClass,
81              Parameter parameter,
82              PlexusConfiguration pomConfiguration,
83              ExpressionEvaluator expressionEvaluator) {
84          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
85  
86          if (isValueSet(config, expressionEvaluator)) {
87              pluginValidationManager.reportPluginMojoValidationIssue(
88                      PluginValidationManager.IssueLocality.INTERNAL,
89                      mavenSession,
90                      mojoDescriptor,
91                      mojoClass,
92                      formatParameter(parameter));
93          }
94      }
95  
96      private String logDeprecatedMojo(MojoDescriptor mojoDescriptor) {
97          return MessageUtils.buffer()
98                  .warning("Goal '")
99                  .warning(mojoDescriptor.getGoal())
100                 .warning("' is deprecated: ")
101                 .warning(mojoDescriptor.getDeprecated())
102                 .toString();
103     }
104 }