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.Optional;
25 import java.util.Properties;
26
27 import org.apache.maven.api.Project;
28 import org.apache.maven.api.Session;
29 import org.apache.maven.internal.impl.DefaultMojoExecution;
30 import org.apache.maven.internal.impl.DefaultSession;
31 import org.apache.maven.plugin.descriptor.MojoDescriptor;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
34 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
35 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class PluginParameterExpressionEvaluatorV4 implements TypeAwareExpressionEvaluator {
77 private Session session;
78
79 private MojoExecution mojoExecution;
80
81 private Project project;
82
83 private Path basedir;
84
85 private Properties properties;
86
87 public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
88 this(session, project, null);
89 }
90
91 public PluginParameterExpressionEvaluatorV4(Session session, Project project, MojoExecution mojoExecution) {
92 this.session = session;
93 this.mojoExecution = mojoExecution;
94 this.properties = new Properties();
95 this.project = project;
96
97
98
99
100
101 this.properties.putAll(session.getUserProperties());
102 this.properties.putAll(session.getSystemProperties());
103
104 Path basedir = null;
105
106 if (project != null) {
107 Optional<Path> projectFile = project.getBasedir();
108
109
110 if (projectFile.isPresent()) {
111 basedir = projectFile.get().toAbsolutePath();
112 }
113 }
114
115 if (basedir == null) {
116 basedir = session.getExecutionRootDirectory();
117 }
118
119 if (basedir == null) {
120 basedir = Paths.get(System.getProperty("user.dir"));
121 }
122
123 this.basedir = basedir;
124 }
125
126 @Override
127 public Object evaluate(String expr) throws ExpressionEvaluationException {
128 return evaluate(expr, null);
129 }
130
131 @Override
132 @SuppressWarnings("checkstyle:methodlength")
133 public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
134 Object value = null;
135
136 if (expr == null) {
137 return null;
138 }
139
140 String expression = stripTokens(expr);
141 if (expression.equals(expr)) {
142 int index = expr.indexOf("${");
143 if (index >= 0) {
144 int lastIndex = expr.indexOf('}', index);
145 if (lastIndex >= 0) {
146 String retVal = expr.substring(0, index);
147
148 if ((index > 0) && (expr.charAt(index - 1) == '$')) {
149 retVal += expr.substring(index + 1, lastIndex + 1);
150 } else {
151 Object subResult = evaluate(expr.substring(index, lastIndex + 1));
152
153 if (subResult != null) {
154 retVal += subResult;
155 } else {
156 retVal += "$" + expr.substring(index + 1, lastIndex + 1);
157 }
158 }
159
160 retVal += evaluate(expr.substring(lastIndex + 1));
161 return retVal;
162 }
163 }
164
165
166 if (expression.contains("$$")) {
167 return expression.replaceAll("\\$\\$", "\\$");
168 } else {
169 return expression;
170 }
171 }
172
173 if ("localRepository".equals(expression)) {
174
175 value = session.getLocalRepository();
176 } else if ("session".equals(expression)) {
177 value = session;
178 } else if (expression.startsWith("session")) {
179
180 try {
181 int pathSeparator = expression.indexOf('/');
182
183 if (pathSeparator > 0) {
184 String pathExpression = expression.substring(1, pathSeparator);
185 value = ReflectionValueExtractor.evaluate(pathExpression, session);
186 value = value + expression.substring(pathSeparator);
187 } else {
188 value = ReflectionValueExtractor.evaluate(expression.substring(1), session);
189 }
190 } catch (Exception e) {
191
192 throw new ExpressionEvaluationException(
193 "Error evaluating plugin parameter expression: " + expression, e);
194 }
195 } else if ("reactorProjects".equals(expression)) {
196 value = session.getProjects();
197 } else if ("project".equals(expression)) {
198 value = project;
199 } else if ("executedProject".equals(expression)) {
200 value = ((DefaultSession) session)
201 .getProject(((DefaultSession) session)
202 .getMavenSession()
203 .getCurrentProject()
204 .getExecutionProject());
205 } else if (expression.startsWith("project") || expression.startsWith("pom")) {
206
207 try {
208 int pathSeparator = expression.indexOf('/');
209
210 if (pathSeparator > 0) {
211 String pathExpression = expression.substring(0, pathSeparator);
212 value = ReflectionValueExtractor.evaluate(pathExpression, project);
213 value = value + expression.substring(pathSeparator);
214 } else {
215 value = ReflectionValueExtractor.evaluate(expression.substring(1), project);
216 }
217 } catch (Exception e) {
218
219 throw new ExpressionEvaluationException(
220 "Error evaluating plugin parameter expression: " + expression, e);
221 }
222 } else if (expression.equals("repositorySystemSession")) {
223
224 } else if (expression.equals("mojo") || expression.equals("mojoExecution")) {
225 value = new DefaultMojoExecution(mojoExecution);
226 } else if (expression.startsWith("mojo")) {
227
228 try {
229 int pathSeparator = expression.indexOf('/');
230
231 if (pathSeparator > 0) {
232 String pathExpression = expression.substring(1, pathSeparator);
233 value = ReflectionValueExtractor.evaluate(pathExpression, mojoExecution);
234 value = value + expression.substring(pathSeparator);
235 } else {
236 value = ReflectionValueExtractor.evaluate(expression.substring(1), mojoExecution);
237 }
238 } catch (Exception e) {
239
240 throw new ExpressionEvaluationException(
241 "Error evaluating plugin parameter expression: " + expression, e);
242 }
243 } else if (expression.equals("plugin")) {
244
245 value = mojoExecution.getMojoDescriptor().getPluginDescriptor();
246 } else if (expression.startsWith("plugin")) {
247
248 try {
249 int pathSeparator = expression.indexOf('/');
250
251 PluginDescriptor pluginDescriptor =
252 mojoExecution.getMojoDescriptor().getPluginDescriptor();
253
254 if (pathSeparator > 0) {
255 String pathExpression = expression.substring(1, pathSeparator);
256 value = ReflectionValueExtractor.evaluate(pathExpression, pluginDescriptor);
257 value = value + expression.substring(pathSeparator);
258 } else {
259 value = ReflectionValueExtractor.evaluate(expression.substring(1), pluginDescriptor);
260 }
261 } catch (Exception e) {
262 throw new ExpressionEvaluationException(
263 "Error evaluating plugin parameter expression: " + expression, e);
264 }
265 } else if ("settings".equals(expression)) {
266 value = session.getSettings();
267 } else if (expression.startsWith("settings")) {
268 try {
269 int pathSeparator = expression.indexOf('/');
270
271 if (pathSeparator > 0) {
272 String pathExpression = expression.substring(1, pathSeparator);
273 value = ReflectionValueExtractor.evaluate(pathExpression, session.getSettings());
274 value = value + expression.substring(pathSeparator);
275 } else {
276 value = ReflectionValueExtractor.evaluate(expression.substring(1), session.getSettings());
277 }
278 } catch (Exception e) {
279
280 throw new ExpressionEvaluationException(
281 "Error evaluating plugin parameter expression: " + expression, e);
282 }
283 } else if ("basedir".equals(expression)) {
284 value = basedir.toString();
285 } else if (expression.startsWith("basedir")) {
286 int pathSeparator = expression.indexOf('/');
287
288 if (pathSeparator > 0) {
289 value = basedir.toString() + expression.substring(pathSeparator);
290 }
291 }
292
293
294
295
296
297
298
299
300
301 if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
302 value = null;
303 }
304
305 if (value == null) {
306
307
308 if (properties != null) {
309
310
311
312
313
314 value = properties.getProperty(expression);
315 }
316
317 if ((value == null) && ((project != null) && (project.getModel().getProperties() != null))) {
318 value = project.getModel().getProperties().get(expression);
319 }
320 }
321
322 if (value instanceof String) {
323
324
325 String val = (String) value;
326
327 int exprStartDelimiter = val.indexOf("${");
328
329 if (exprStartDelimiter >= 0) {
330 if (exprStartDelimiter > 0) {
331 value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
332 } else {
333 value = evaluate(val.substring(exprStartDelimiter));
334 }
335 }
336 }
337
338 return value;
339 }
340
341 private static boolean isTypeCompatible(Class<?> type, Object value) {
342 if (type.isInstance(value)) {
343 return true;
344 }
345
346 return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
347 && value.getClass().getName().startsWith("java.lang."));
348 }
349
350 private String stripTokens(String expr) {
351 if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
352 expr = expr.substring(2, expr.length() - 1);
353 }
354 return expr;
355 }
356
357 @Override
358 public File alignToBaseDirectory(File file) {
359
360
361 if (file != null) {
362 if (file.isAbsolute()) {
363
364 } else if (file.getPath().startsWith(File.separator)) {
365
366 file = file.getAbsoluteFile();
367 } else {
368
369 file = basedir.resolve(file.getPath())
370 .normalize()
371 .toAbsolutePath()
372 .toFile();
373 }
374 }
375 return file;
376 }
377 }