1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
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 }