1 | |
package org.apache.maven.plugin; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.execution.MavenSession; |
23 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
24 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
25 | |
import org.apache.maven.project.MavenProject; |
26 | |
import org.apache.maven.project.path.PathTranslator; |
27 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; |
28 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; |
29 | |
import org.codehaus.plexus.logging.Logger; |
30 | |
import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.Map; |
35 | |
import java.util.Properties; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class PluginParameterExpressionEvaluator |
43 | |
implements ExpressionEvaluator |
44 | |
{ |
45 | |
private static final Map BANNED_EXPRESSIONS; |
46 | |
|
47 | |
private static final Map DEPRECATED_EXPRESSIONS; |
48 | |
|
49 | |
static |
50 | |
{ |
51 | 1 | Map deprecated = new HashMap(); |
52 | |
|
53 | 1 | deprecated.put( "project.build.resources", "project.resources" ); |
54 | 1 | deprecated.put( "project.build.testResources", "project.testResources" ); |
55 | |
|
56 | 1 | DEPRECATED_EXPRESSIONS = deprecated; |
57 | |
|
58 | 1 | Map banned = new HashMap(); |
59 | |
|
60 | 1 | BANNED_EXPRESSIONS = banned; |
61 | 1 | } |
62 | |
|
63 | |
private final PathTranslator pathTranslator; |
64 | |
|
65 | |
private final MavenSession context; |
66 | |
|
67 | |
private final Logger logger; |
68 | |
|
69 | |
private final MojoExecution mojoExecution; |
70 | |
|
71 | |
private final MavenProject project; |
72 | |
|
73 | |
private final String basedir; |
74 | |
|
75 | |
private final Properties properties; |
76 | |
|
77 | |
public PluginParameterExpressionEvaluator( MavenSession context, |
78 | |
MojoExecution mojoExecution, |
79 | |
PathTranslator pathTranslator, |
80 | |
Logger logger, |
81 | |
MavenProject project, |
82 | |
Properties properties ) |
83 | 11 | { |
84 | 11 | this.context = context; |
85 | 11 | this.mojoExecution = mojoExecution; |
86 | 11 | this.pathTranslator = pathTranslator; |
87 | 11 | this.logger = logger; |
88 | 11 | this.project = project; |
89 | 11 | this.properties = properties; |
90 | |
|
91 | 11 | String basedir = null; |
92 | |
|
93 | 11 | if ( project != null ) |
94 | |
{ |
95 | 8 | File baseDir = project.getBasedir(); |
96 | |
|
97 | |
|
98 | 8 | if ( baseDir != null ) |
99 | |
{ |
100 | 1 | basedir = baseDir.getAbsolutePath(); |
101 | |
} |
102 | |
} |
103 | |
|
104 | 11 | if ( basedir == null ) |
105 | |
{ |
106 | 10 | basedir = System.getProperty( "user.dir" ); |
107 | |
} |
108 | |
|
109 | 11 | this.basedir = basedir; |
110 | 11 | } |
111 | |
|
112 | |
public Object evaluate( String expr ) |
113 | |
throws ExpressionEvaluationException |
114 | |
{ |
115 | 25 | Object value = null; |
116 | |
|
117 | 25 | if ( expr == null ) |
118 | |
{ |
119 | 0 | return null; |
120 | |
} |
121 | |
|
122 | 25 | String expression = stripTokens( expr ); |
123 | 25 | if ( expression.equals( expr ) ) |
124 | |
{ |
125 | 13 | int index = expr.indexOf( "${" ); |
126 | 13 | if ( index >= 0 ) |
127 | |
{ |
128 | 8 | int lastIndex = expr.indexOf( "}", index ); |
129 | 8 | if ( lastIndex >= 0 ) |
130 | |
{ |
131 | 8 | String retVal = expr.substring( 0, index ); |
132 | |
|
133 | 8 | if ( index > 0 && expr.charAt( index - 1 ) == '$' ) |
134 | |
{ |
135 | 2 | retVal += expr.substring( index + 1, lastIndex + 1 ); |
136 | |
} |
137 | |
else |
138 | |
{ |
139 | 6 | retVal += evaluate( expr.substring( index, lastIndex + 1 ) ); |
140 | |
} |
141 | |
|
142 | 8 | retVal += evaluate( expr.substring( lastIndex + 1 ) ); |
143 | 8 | return retVal; |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
|
148 | 5 | if ( expression.indexOf( "$$" ) > -1 ) |
149 | |
{ |
150 | 0 | return expression.replaceAll( "\\$\\$", "\\$" ); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 5 | return expression; |
155 | |
} |
156 | |
} |
157 | |
|
158 | 12 | MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); |
159 | 12 | if ( BANNED_EXPRESSIONS.containsKey( expression ) ) |
160 | |
{ |
161 | 0 | throw new ExpressionEvaluationException( "The parameter expression: \'" + expression + |
162 | |
"\' used in mojo: \'" + mojoDescriptor.getGoal() + "\' is banned. Use \'" + |
163 | |
BANNED_EXPRESSIONS.get( expression ) + "\' instead." ); |
164 | |
} |
165 | 12 | else if ( DEPRECATED_EXPRESSIONS.containsKey( expression ) ) |
166 | |
{ |
167 | 0 | logger.warn( "The parameter expression: \'" + expression + "\' used in mojo: \'" + |
168 | |
mojoDescriptor.getGoal() + "\' has been deprecated. Use \'" + DEPRECATED_EXPRESSIONS.get( expression ) + |
169 | |
"\' instead." ); |
170 | |
} |
171 | |
|
172 | 12 | if ( "localRepository".equals( expression ) ) |
173 | |
{ |
174 | 1 | value = context.getLocalRepository(); |
175 | |
} |
176 | 11 | else if ( "session".equals( expression ) ) |
177 | |
{ |
178 | 0 | value = context; |
179 | |
} |
180 | 11 | else if ( "reactorProjects".equals( expression ) ) |
181 | |
{ |
182 | 0 | value = context.getSortedProjects(); |
183 | |
} |
184 | 11 | else if ( "reports".equals( expression ) ) |
185 | |
{ |
186 | 0 | value = mojoExecution.getReports(); |
187 | |
} |
188 | 11 | else if ("mojoExecution".equals(expression)) |
189 | |
{ |
190 | 0 | value = mojoExecution; |
191 | |
} |
192 | 11 | else if ( "project".equals( expression ) ) |
193 | |
{ |
194 | 0 | value = project; |
195 | |
} |
196 | 11 | else if ( "executedProject".equals( expression ) ) |
197 | |
{ |
198 | 0 | value = project.getExecutionProject(); |
199 | |
} |
200 | 11 | else if ( expression.startsWith( "project" ) ) |
201 | |
{ |
202 | |
try |
203 | |
{ |
204 | 6 | int pathSeparator = expression.indexOf( "/" ); |
205 | |
|
206 | 6 | if ( pathSeparator > 0 ) |
207 | |
{ |
208 | 0 | String pathExpression = expression.substring( 0, pathSeparator ); |
209 | 0 | value = ReflectionValueExtractor.evaluate( pathExpression, project ); |
210 | 0 | value = value + expression.substring( pathSeparator ); |
211 | 0 | } |
212 | |
else |
213 | |
{ |
214 | 6 | value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project ); |
215 | |
} |
216 | |
} |
217 | 0 | catch ( Exception e ) |
218 | |
{ |
219 | |
|
220 | 0 | throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression, |
221 | |
e ); |
222 | 6 | } |
223 | |
} |
224 | 5 | else if ( expression.startsWith( "plugin" ) ) |
225 | |
{ |
226 | |
try |
227 | |
{ |
228 | 1 | int pathSeparator = expression.indexOf( "/" ); |
229 | |
|
230 | 1 | PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); |
231 | |
|
232 | 1 | if ( pathSeparator > 0 ) |
233 | |
{ |
234 | 0 | String pathExpression = expression.substring( 1, pathSeparator ); |
235 | 0 | value = ReflectionValueExtractor.evaluate( pathExpression, pluginDescriptor ); |
236 | 0 | value = value + expression.substring( pathSeparator ); |
237 | 0 | } |
238 | |
else |
239 | |
{ |
240 | 1 | value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor ); |
241 | |
} |
242 | |
} |
243 | 0 | catch ( Exception e ) |
244 | |
{ |
245 | |
|
246 | 0 | throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression, |
247 | |
e ); |
248 | 1 | } |
249 | |
} |
250 | 4 | else if ( "settings".equals( expression ) ) |
251 | |
{ |
252 | 0 | value = context.getSettings(); |
253 | |
} |
254 | 4 | else if ( expression.startsWith( "settings" ) ) |
255 | |
{ |
256 | |
try |
257 | |
{ |
258 | 0 | int pathSeparator = expression.indexOf( "/" ); |
259 | |
|
260 | 0 | if ( pathSeparator > 0 ) |
261 | |
{ |
262 | 0 | String pathExpression = expression.substring( 1, pathSeparator ); |
263 | 0 | value = ReflectionValueExtractor.evaluate( pathExpression, context.getSettings() ); |
264 | 0 | value = value + expression.substring( pathSeparator ); |
265 | 0 | } |
266 | |
else |
267 | |
{ |
268 | 0 | value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), context.getSettings() ); |
269 | |
} |
270 | |
} |
271 | 0 | catch ( Exception e ) |
272 | |
{ |
273 | |
|
274 | 0 | throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression, |
275 | |
e ); |
276 | 0 | } |
277 | |
} |
278 | 4 | else if ( "basedir".equals( expression ) ) |
279 | |
{ |
280 | 1 | value = basedir; |
281 | |
} |
282 | 3 | else if ( expression.startsWith( "basedir" ) ) |
283 | |
{ |
284 | 0 | int pathSeparator = expression.indexOf( "/" ); |
285 | |
|
286 | 0 | if ( pathSeparator > 0 ) |
287 | |
{ |
288 | 0 | value = basedir + expression.substring( pathSeparator ); |
289 | |
} |
290 | |
else |
291 | |
{ |
292 | 0 | logger.error( "Got expression '" + expression + "' that was not recognised" ); |
293 | |
} |
294 | |
} |
295 | |
|
296 | 12 | if ( value == null ) |
297 | |
{ |
298 | |
|
299 | |
|
300 | 3 | if ( value == null && properties != null ) |
301 | |
{ |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | 3 | value = properties.getProperty( expression ); |
308 | |
} |
309 | |
|
310 | 3 | if ( value == null && project != null && project.getProperties() != null ) |
311 | |
{ |
312 | 1 | value = project.getProperties().getProperty( expression ); |
313 | |
} |
314 | |
|
315 | |
} |
316 | |
|
317 | 12 | if ( value instanceof String ) |
318 | |
{ |
319 | |
|
320 | |
|
321 | 10 | String val = (String) value; |
322 | |
|
323 | 10 | int exprStartDelimiter = val.indexOf( "${" ); |
324 | |
|
325 | 10 | if ( exprStartDelimiter >= 0 ) |
326 | |
{ |
327 | 0 | if ( exprStartDelimiter > 0 ) |
328 | |
{ |
329 | 0 | value = val.substring( 0, exprStartDelimiter ) + evaluate( val.substring( exprStartDelimiter ) ); |
330 | |
} |
331 | |
else |
332 | |
{ |
333 | 0 | value = evaluate( val.substring( exprStartDelimiter ) ); |
334 | |
} |
335 | |
} |
336 | |
} |
337 | |
|
338 | 12 | return value; |
339 | |
} |
340 | |
|
341 | |
private String stripTokens( String expr ) |
342 | |
{ |
343 | 25 | if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 ) |
344 | |
{ |
345 | 12 | expr = expr.substring( 2, expr.length() - 1 ); |
346 | |
} |
347 | 25 | return expr; |
348 | |
} |
349 | |
|
350 | |
public File alignToBaseDirectory( File file ) |
351 | |
{ |
352 | |
File basedir; |
353 | |
|
354 | 0 | if ( project != null && project.getFile() != null ) |
355 | |
{ |
356 | 0 | basedir = project.getFile().getParentFile(); |
357 | |
} |
358 | |
else |
359 | |
{ |
360 | 0 | basedir = new File( "." ).getAbsoluteFile().getParentFile(); |
361 | |
} |
362 | |
|
363 | 0 | return new File( pathTranslator.alignToBaseDirectory( file.getPath(), basedir ) ); |
364 | |
} |
365 | |
|
366 | |
} |