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