Coverage Report - org.apache.maven.plugin.PluginParameterExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginParameterExpressionEvaluator
0%
0/123
0%
0/82
12.75
 
 1  
 package org.apache.maven.plugin;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 39  
  * @version $Id: PluginParameterExpressionEvaluator.java 565812 2007-08-14 15:33:44Z jdcasey $
 40  
  * @todo belong in MavenSession, so it only gets created once?
 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  0
         Map deprecated = new HashMap();
 52  
 
 53  0
         deprecated.put( "project.build.resources", "project.resources" );
 54  0
         deprecated.put( "project.build.testResources", "project.testResources" );
 55  
 
 56  0
         DEPRECATED_EXPRESSIONS = deprecated;
 57  
 
 58  0
         Map banned = new HashMap();
 59  
 
 60  0
         BANNED_EXPRESSIONS = banned;
 61  0
     }
 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  0
     {
 84  0
         this.context = context;
 85  0
         this.mojoExecution = mojoExecution;
 86  0
         this.pathTranslator = pathTranslator;
 87  0
         this.logger = logger;
 88  0
         this.project = project;
 89  0
         this.properties = properties;
 90  
 
 91  0
         String basedir = null;
 92  
 
 93  0
         if ( project != null )
 94  
         {
 95  0
             File projectFile = project.getFile();
 96  
 
 97  
             // this should always be the case for non-super POM instances...
 98  0
             if ( projectFile != null )
 99  
             {
 100  0
                 basedir = projectFile.getParentFile().getAbsolutePath();
 101  
             }
 102  
         }
 103  
 
 104  0
         if ( basedir == null )
 105  
         {
 106  0
             basedir = System.getProperty( "user.dir" );
 107  
         }
 108  
 
 109  0
         this.basedir = basedir;
 110  0
     }
 111  
 
 112  
     public Object evaluate( String expr )
 113  
         throws ExpressionEvaluationException
 114  
     {
 115  0
         Object value = null;
 116  
 
 117  0
         if ( expr == null )
 118  
         {
 119  0
             return null;
 120  
         }
 121  
         
 122  0
         String expression = stripTokens( expr );
 123  0
         if ( expression.equals( expr ) )
 124  
         {
 125  0
             int index = expr.indexOf( "${" );
 126  0
             if ( index >= 0 )
 127  
             {
 128  0
                 int lastIndex = expr.indexOf( "}", index );
 129  0
                 if ( lastIndex >= 0 )
 130  
                 {
 131  0
                     String retVal = expr.substring( 0, index );
 132  
                     
 133  0
                     if ( index > 0 && expr.charAt( index - 1 ) == '$' )
 134  
                     {
 135  0
                         retVal += expr.substring( index + 1, lastIndex + 1 );
 136  
                     }
 137  
                     else
 138  
                     {
 139  0
                         retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
 140  
                     }
 141  
                     
 142  0
                     retVal += evaluate( expr.substring( lastIndex + 1 ) );
 143  0
                     return retVal;
 144  
                 }
 145  
             }
 146  
 
 147  
             // Was not an expression
 148  0
             if ( expression.indexOf( "$$" ) > -1 )
 149  
             {
 150  0
                 return expression.replaceAll( "\\$\\$", "\\$" );
 151  
             }
 152  
             else
 153  
             {
 154  0
                 return expression;
 155  
             }
 156  
         }
 157  
 
 158  0
         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
 159  0
         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  0
         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  0
         if ( "localRepository".equals( expression ) )
 173  
         {
 174  0
             value = context.getLocalRepository();
 175  
         }
 176  0
         else if ( "session".equals( expression ) )
 177  
         {
 178  0
             value = context;
 179  
         }
 180  0
         else if ( "reactorProjects".equals( expression ) )
 181  
         {
 182  0
             value = context.getSortedProjects();
 183  
         }
 184  0
         else if ( "reports".equals( expression ) )
 185  
         {
 186  0
             value = mojoExecution.getReports();
 187  
         }
 188  0
         else if ("mojoExecution".equals(expression)) 
 189  
         {
 190  0
                 value = mojoExecution;
 191  
         }
 192  0
         else if ( "project".equals( expression ) )
 193  
         {
 194  0
             value = project;
 195  
         }
 196  0
         else if ( "executedProject".equals( expression ) )
 197  
         {
 198  0
             value = project.getExecutionProject();
 199  
         }
 200  0
         else if ( expression.startsWith( "project" ) )
 201  
         {
 202  
             try
 203  
             {
 204  0
                 int pathSeparator = expression.indexOf( "/" );
 205  
 
 206  0
                 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  0
                     value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project );
 215  
                 }
 216  
             }
 217  0
             catch ( Exception e )
 218  
             {
 219  
                 // TODO: don't catch exception
 220  0
                 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
 221  
                                                          e );
 222  0
             }
 223  
         }
 224  0
         else if ( expression.startsWith( "plugin" ) )
 225  
         {
 226  
             try
 227  
             {
 228  0
                 int pathSeparator = expression.indexOf( "/" );
 229  
 
 230  0
                 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
 231  
 
 232  0
                 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  0
                     value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor );
 241  
                 }
 242  
             }
 243  0
             catch ( Exception e )
 244  
             {
 245  
                 // TODO: don't catch exception
 246  0
                 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
 247  
                                                          e );
 248  0
             }
 249  
         }
 250  0
         else if ( "settings".equals( expression ) )
 251  
         {
 252  0
             value = context.getSettings();
 253  
         }
 254  0
         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  
                 // TODO: don't catch exception
 274  0
                 throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
 275  
                                                          e );
 276  0
             }
 277  
         }
 278  0
         else if ( "basedir".equals( expression ) )
 279  
         {
 280  0
             value = basedir;
 281  
         }
 282  0
         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  0
         if ( value == null )
 297  
         {
 298  
             // Check POM-level properties before we default over to system properties.
 299  0
             if ( project != null && project.getProperties() != null )
 300  
             {
 301  0
                 value = project.getProperties().getProperty( expression );
 302  
             }
 303  
 
 304  0
             if ( value == null && properties != null )
 305  
             {
 306  
                 // We will attempt to get nab a system property as a way to specify a
 307  
                 // parameter to a plugins. My particular case here is allowing the surefire
 308  
                 // plugin to run a single test so I want to specify that class on the cli
 309  
                 // as a parameter.
 310  
 
 311  0
                 value = properties.getProperty( expression );
 312  
             }
 313  
         }
 314  
 
 315  0
         if ( value instanceof String )
 316  
         {
 317  
             // TODO: without #, this could just be an evaluate call...
 318  
 
 319  0
             String val = (String) value;
 320  
 
 321  0
             int exprStartDelimiter = val.indexOf( "${" );
 322  
 
 323  0
             if ( exprStartDelimiter >= 0 )
 324  
             {
 325  0
                 if ( exprStartDelimiter > 0 )
 326  
                 {
 327  0
                     value = val.substring( 0, exprStartDelimiter ) + evaluate( val.substring( exprStartDelimiter ) );
 328  
                 }
 329  
                 else
 330  
                 {
 331  0
                     value = evaluate( val.substring( exprStartDelimiter ) );
 332  
                 }
 333  
             }
 334  
         }
 335  
 
 336  0
         return value;
 337  
     }
 338  
 
 339  
     private String stripTokens( String expr )
 340  
     {
 341  0
         if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
 342  
         {
 343  0
             expr = expr.substring( 2, expr.length() - 1 );
 344  
         }
 345  0
         return expr;
 346  
     }
 347  
 
 348  
     public File alignToBaseDirectory( File file )
 349  
     {
 350  
         File basedir;
 351  
 
 352  0
         if ( project != null && project.getFile() != null )
 353  
         {
 354  0
             basedir = project.getFile().getParentFile();
 355  
         }
 356  
         else
 357  
         {
 358  0
             basedir = new File( "." ).getAbsoluteFile().getParentFile();
 359  
         }
 360  
 
 361  0
         return new File( pathTranslator.alignToBaseDirectory( file.getPath(), basedir ) );
 362  
     }
 363  
 
 364  
 }