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