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.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.plugin.descriptor.MojoDescriptor;
25  import org.apache.maven.plugin.descriptor.Parameter;
26  import org.apache.maven.shared.utils.logging.MessageUtils;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
28  import org.codehaus.plexus.configuration.PlexusConfiguration;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Print warnings if deprecated mojo or parameters of plugin are used in configuration.
34   *
35   * @author Slawomir Jaranowski
36   */
37  @Named
38  @Singleton
39  class DeprecatedPluginValidator extends AbstractMavenPluginParametersValidator {
40      private static final Logger LOGGER = LoggerFactory.getLogger(DeprecatedPluginValidator.class);
41  
42      @Override
43      protected Logger getLogger() {
44          return LOGGER;
45      }
46  
47      @Override
48      protected String getParameterLogReason(Parameter parameter) {
49          return "is deprecated: " + parameter.getDeprecated();
50      }
51  
52      @Override
53      public void validate(
54              MojoDescriptor mojoDescriptor,
55              PlexusConfiguration pomConfiguration,
56              ExpressionEvaluator expressionEvaluator) {
57          if (!LOGGER.isWarnEnabled()) {
58              return;
59          }
60  
61          if (mojoDescriptor.getDeprecated() != null) {
62              logDeprecatedMojo(mojoDescriptor);
63          }
64  
65          mojoDescriptor.getParameters().stream()
66                  .filter(parameter -> parameter.getDeprecated() != null)
67                  .filter(Parameter::isEditable)
68                  .forEach(parameter -> checkParameter(parameter, pomConfiguration, expressionEvaluator));
69      }
70  
71      private void checkParameter(
72              Parameter parameter, PlexusConfiguration pomConfiguration, ExpressionEvaluator expressionEvaluator) {
73          PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);
74  
75          if (isValueSet(config, expressionEvaluator)) {
76              logParameter(parameter);
77          }
78      }
79  
80      private void logDeprecatedMojo(MojoDescriptor mojoDescriptor) {
81          String message = MessageUtils.buffer()
82                  .warning("Goal '")
83                  .warning(mojoDescriptor.getGoal())
84                  .warning("' is deprecated: ")
85                  .warning(mojoDescriptor.getDeprecated())
86                  .toString();
87  
88          LOGGER.warn(message);
89      }
90  }