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                      mavenSession, mojoDescriptor, mojoClass, logDeprecatedMojo(mojoDescriptor));
62          }
63  
64          if (mojoDescriptor.getParameters() != null) {
65              mojoDescriptor.getParameters().stream()
66                      .filter(parameter -> parameter.getDeprecated() != null)
67                      .filter(Parameter::isEditable)
68                      .forEach(parameter -> checkParameter(
69                              mavenSession, mojoDescriptor, mojoClass, parameter, pomConfiguration, expressionEvaluator));
70          }
71      }
72  
73      private void checkParameter(
74              MavenSession mavenSession,
75              MojoDescriptor mojoDescriptor,
76              Class<?> mojoClass,
77              Parameter parameter,
78              PlexusConfiguration pomConfiguration,
79              ExpressionEvaluator expressionEvaluator) {
80          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
81  
82          if (isValueSet(config, expressionEvaluator)) {
83              pluginValidationManager.reportPluginMojoValidationIssue(
84                      mavenSession, mojoDescriptor, mojoClass, formatParameter(parameter));
85          }
86      }
87  
88      private String logDeprecatedMojo(MojoDescriptor mojoDescriptor) {
89          return MessageUtils.buffer()
90                  .warning("Goal '")
91                  .warning(mojoDescriptor.getGoal())
92                  .warning("' is deprecated: ")
93                  .warning(mojoDescriptor.getDeprecated())
94                  .toString();
95      }
96  }