1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin;
20
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Properties;
28
29 import org.apache.maven.api.MojoExecution;
30 import org.apache.maven.api.Project;
31 import org.apache.maven.api.Session;
32 import org.apache.maven.model.interpolation.reflection.ReflectionValueExtractor;
33 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
34 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class PluginParameterExpressionEvaluatorV4 implements TypeAwareExpressionEvaluator {
57 private Session session;
58
59 private MojoExecution mojoExecution;
60
61 private Project project;
62
63 private Path basedir;
64
65 private Properties properties;
66
67 public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
68 this(session, project, null);
69 }
70
71 public PluginParameterExpressionEvaluatorV4(Session session, Project project, MojoExecution mojoExecution) {
72 this.session = session;
73 this.mojoExecution = mojoExecution;
74 this.properties = new Properties();
75 this.project = project;
76
77
78
79
80
81 this.properties.putAll(session.getUserProperties());
82 this.properties.putAll(session.getSystemProperties());
83
84 Path basedir = null;
85
86 if (project != null) {
87 Optional<Path> projectFile = project.getBasedir();
88
89
90 if (projectFile.isPresent()) {
91 basedir = projectFile.get().toAbsolutePath();
92 }
93 }
94
95 if (basedir == null) {
96 basedir = session.getTopDirectory();
97 }
98
99 if (basedir == null) {
100 basedir = Paths.get(System.getProperty("user.dir"));
101 }
102
103 this.basedir = basedir;
104 }
105
106 @Override
107 public Object evaluate(String expr) throws ExpressionEvaluationException {
108 return evaluate(expr, null);
109 }
110
111 @Override
112 @SuppressWarnings("checkstyle:methodlength")
113 public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
114 Object value = null;
115
116 if (expr == null) {
117 return null;
118 }
119
120 String expression = stripTokens(expr);
121 if (expression.equals(expr)) {
122 int index = expr.indexOf("${");
123 if (index >= 0) {
124 int lastIndex = expr.indexOf('}', index);
125 if (lastIndex >= 0) {
126 String retVal = expr.substring(0, index);
127
128 if ((index > 0) && (expr.charAt(index - 1) == '$')) {
129 retVal += expr.substring(index + 1, lastIndex + 1);
130 } else {
131 Object subResult = evaluate(expr.substring(index, lastIndex + 1));
132
133 if (subResult != null) {
134 retVal += subResult;
135 } else {
136 retVal += "$" + expr.substring(index + 1, lastIndex + 1);
137 }
138 }
139
140 retVal += evaluate(expr.substring(lastIndex + 1));
141 return retVal;
142 }
143 }
144
145
146 return expression.replace("$$", "$");
147 }
148
149 Map<String, Object> objects = new HashMap<>();
150 objects.put("session.", session);
151 objects.put("project.", project);
152 objects.put("mojo.", mojoExecution);
153 objects.put("settings.", session.getSettings());
154 for (Map.Entry<String, Object> ctx : objects.entrySet()) {
155 if (expression.startsWith(ctx.getKey())) {
156 try {
157 int pathSeparator = expression.indexOf('/');
158 if (pathSeparator > 0) {
159 String pathExpression = expression.substring(0, pathSeparator);
160 value = ReflectionValueExtractor.evaluate(pathExpression, ctx.getValue());
161 if (pathSeparator < expression.length() - 1) {
162 if (value instanceof Path) {
163 value = ((Path) value).resolve(expression.substring(pathSeparator + 1));
164 } else {
165 value = value + expression.substring(pathSeparator);
166 }
167 }
168 } else {
169 value = ReflectionValueExtractor.evaluate(expression, ctx.getValue());
170 }
171 break;
172 } catch (Exception e) {
173
174 throw new ExpressionEvaluationException(
175 "Error evaluating plugin parameter expression: " + expression, e);
176 }
177 }
178 }
179
180
181
182
183
184
185
186
187
188 if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
189 value = null;
190 }
191
192 if (value == null) {
193
194
195 if (properties != null) {
196
197
198
199
200
201 value = properties.getProperty(expression);
202 }
203
204 if ((value == null) && ((project != null) && (project.getModel().getProperties() != null))) {
205 value = project.getModel().getProperties().get(expression);
206 }
207 }
208
209 if (value instanceof String) {
210
211
212 String val = (String) value;
213
214 int exprStartDelimiter = val.indexOf("${");
215
216 if (exprStartDelimiter >= 0) {
217 if (exprStartDelimiter > 0) {
218 value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
219 } else {
220 value = evaluate(val.substring(exprStartDelimiter));
221 }
222 }
223 }
224
225 return value;
226 }
227
228 private static boolean isTypeCompatible(Class<?> type, Object value) {
229 if (type.isInstance(value)) {
230 return true;
231 }
232
233 return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
234 && value.getClass().getName().startsWith("java.lang."));
235 }
236
237 private String stripTokens(String expr) {
238 if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
239 expr = expr.substring(2, expr.length() - 1);
240 }
241 return expr;
242 }
243
244 @Override
245 public File alignToBaseDirectory(File file) {
246
247
248 if (file != null) {
249 if (file.isAbsolute()) {
250
251 } else if (file.getPath().startsWith(File.separator)) {
252
253 file = file.getAbsoluteFile();
254 } else {
255
256 file = basedir.resolve(file.getPath())
257 .normalize()
258 .toAbsolutePath()
259 .toFile();
260 }
261 }
262 return file;
263 }
264 }