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 java.util.HashMap;
26  import java.util.Objects;
27  
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.PluginValidationManager;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.Parameter;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  
35  /**
36   * Print warnings if deprecated core parameters are used in mojo.
37   *
38   * @since 3.9.1
39   */
40  @Singleton
41  @Named
42  class DeprecatedCoreExpressionValidator extends AbstractMavenPluginParametersValidator {
43      private static final HashMap<String, String> DEPRECATED_CORE_PARAMETERS;
44  
45      private static final String ARTIFACT_REPOSITORY_REASON =
46              "ArtifactRepository type is deprecated and its use in Mojos should be avoided.";
47  
48      static {
49          HashMap<String, String> deprecatedCoreParameters = new HashMap<>();
50          deprecatedCoreParameters.put("${localRepository}", ARTIFACT_REPOSITORY_REASON);
51          deprecatedCoreParameters.put("${session.localRepository}", ARTIFACT_REPOSITORY_REASON);
52          DEPRECATED_CORE_PARAMETERS = deprecatedCoreParameters;
53      }
54  
55      @Inject
56      DeprecatedCoreExpressionValidator(PluginValidationManager pluginValidationManager) {
57          super(pluginValidationManager);
58      }
59  
60      @Override
61      protected String getParameterLogReason(Parameter parameter) {
62          return "uses deprecated parameter expression '" + parameter.getDefaultValue() + "': "
63                  + DEPRECATED_CORE_PARAMETERS.get(parameter.getDefaultValue());
64      }
65  
66      @Override
67      protected void doValidate(
68              MavenSession mavenSession,
69              MojoDescriptor mojoDescriptor,
70              Class<?> mojoClass,
71              PlexusConfiguration pomConfiguration,
72              ExpressionEvaluator expressionEvaluator) {
73          if (mojoDescriptor.getParameters() == null) {
74              return;
75          }
76  
77          mojoDescriptor.getParameters().stream()
78                  .filter(this::isDeprecated)
79                  .map(this::formatParameter)
80                  .forEach(m -> pluginValidationManager.reportPluginMojoValidationIssue(
81                          PluginValidationManager.IssueLocality.EXTERNAL, mavenSession, mojoDescriptor, mojoClass, m));
82      }
83  
84      private boolean isDeprecated(Parameter parameter) {
85          return Objects.equals(
86                          org.apache.maven.artifact.repository.ArtifactRepository.class.getName(), parameter.getType())
87                  && DEPRECATED_CORE_PARAMETERS.containsKey(parameter.getDefaultValue());
88      }
89  }