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