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