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 java.util.HashMap;
25  import java.util.Objects;
26  
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.Parameter;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
30  import org.codehaus.plexus.configuration.PlexusConfiguration;
31  
32  /**
33   * Print warnings if deprecated core parameters are used in mojo.
34   *
35   * @since 3.9.1
36   */
37  @Singleton
38  @Named
39  class DeprecatedCoreExpressionValidator extends AbstractMavenPluginParametersValidator {
40      private static final HashMap<String, String> DEPRECATED_CORE_PARAMETERS;
41  
42      private static final String ARTIFACT_REPOSITORY_REASON =
43              "Avoid use of ArtifactRepository type. If you need access to local repository, switch to '${repositorySystemSession}' expression and get LRM from it instead.";
44  
45      static {
46          HashMap<String, String> deprecatedCoreParameters = new HashMap<>();
47          deprecatedCoreParameters.put("${localRepository}", ARTIFACT_REPOSITORY_REASON);
48          deprecatedCoreParameters.put("${session.localRepository}", ARTIFACT_REPOSITORY_REASON);
49          DEPRECATED_CORE_PARAMETERS = deprecatedCoreParameters;
50      }
51  
52      @Override
53      protected String getParameterLogReason(Parameter parameter) {
54          return "is deprecated core expression; " + DEPRECATED_CORE_PARAMETERS.get(parameter.getDefaultValue());
55      }
56  
57      @Override
58      protected void doValidate(
59              MojoDescriptor mojoDescriptor,
60              PlexusConfiguration pomConfiguration,
61              ExpressionEvaluator expressionEvaluator) {
62          if (mojoDescriptor.getParameters() == null) {
63              return;
64          }
65  
66          mojoDescriptor.getParameters().stream().filter(this::isDeprecated).forEach(this::logParameter);
67      }
68  
69      private boolean isDeprecated(Parameter parameter) {
70          return Objects.equals(
71                          org.apache.maven.artifact.repository.ArtifactRepository.class.getName(), parameter.getType())
72                  && DEPRECATED_CORE_PARAMETERS.containsKey(parameter.getDefaultValue());
73      }
74  }