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