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 org.apache.maven.execution.MavenSession;
22 import org.apache.maven.plugin.PluginValidationManager;
23 import org.apache.maven.plugin.descriptor.MojoDescriptor;
24 import org.apache.maven.plugin.descriptor.Parameter;
25 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
26 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
27 import org.codehaus.plexus.configuration.PlexusConfiguration;
28
29 import static java.util.Objects.requireNonNull;
30
31
32
33
34
35 abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator {
36
37 protected final PluginValidationManager pluginValidationManager;
38
39 protected AbstractMavenPluginParametersValidator(PluginValidationManager pluginValidationManager) {
40 this.pluginValidationManager = requireNonNull(pluginValidationManager);
41 }
42
43 protected boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
44 if (config == null) {
45 return false;
46 }
47
48
49 if (config.getChildCount() > 0) {
50 return true;
51 }
52
53 String strValue = config.getValue();
54
55 if (strValue == null || strValue.isEmpty()) {
56 return false;
57 }
58
59 if (isIgnoredProperty(strValue)) {
60 return false;
61 }
62
63
64
65
66 try {
67 return expressionEvaluator.evaluate(strValue) != null;
68 } catch (ExpressionEvaluationException e) {
69
70
71 }
72
73
74 return false;
75 }
76
77 @Override
78 public final void validate(
79 MavenSession mavenSession,
80 MojoDescriptor mojoDescriptor,
81 Class<?> mojoClass,
82 PlexusConfiguration pomConfiguration,
83 ExpressionEvaluator expressionEvaluator) {
84 doValidate(mavenSession, mojoDescriptor, mojoClass, pomConfiguration, expressionEvaluator);
85 }
86
87 protected abstract void doValidate(
88 MavenSession mavenSession,
89 MojoDescriptor mojoDescriptor,
90 Class<?> mojoClass,
91 PlexusConfiguration pomConfiguration,
92 ExpressionEvaluator expressionEvaluator);
93
94 protected boolean isIgnoredProperty(String strValue) {
95 return false;
96 }
97
98 protected abstract String getParameterLogReason(Parameter parameter);
99
100 protected String formatParameter(Parameter parameter) {
101 StringBuilder stringBuilder = new StringBuilder()
102 .append("Parameter '")
103 .append(parameter.getName())
104 .append('\'');
105
106 if (parameter.getExpression() != null) {
107 String userProperty = parameter.getExpression().replace("${", "'").replace('}', '\'');
108 stringBuilder.append(" (user property ").append(userProperty).append(")");
109 }
110
111 stringBuilder.append(" ").append(getParameterLogReason(parameter));
112
113 return stringBuilder.toString();
114 }
115 }