Coverage Report - org.apache.maven.plugin.antrun.AntPropertyHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
AntPropertyHelper
44%
24/54
32%
11/34
4.6
 
 1  
 package org.apache.maven.plugin.antrun;
 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 java.io.File;
 23  
 import java.util.Collections;
 24  
 import java.util.Hashtable;
 25  
 import java.util.Iterator;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.apache.maven.artifact.Artifact;
 30  
 import org.apache.maven.plugin.logging.Log;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.apache.tools.ant.PropertyHelper;
 33  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 34  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 35  
 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
 36  
 
 37  
 /**
 38  
  * Makes the ${expressions} used in Maven available to Ant as properties.
 39  
  *
 40  
  * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
 41  
  */
 42  
 public class AntPropertyHelper
 43  
     extends PropertyHelper
 44  
 {
 45  
     private static final String DEPENDENCY_PREFIX = "maven.dependency.";
 46  
     private Log log;
 47  
     private ExpressionEvaluator exprEvaluator;
 48  
     private MavenProject mavenProject;
 49  3
     private Map artifactMap = new Hashtable();
 50  
 
 51  
     /**
 52  
      * @deprecated use the other constructor
 53  
      * @param project
 54  
      * @param l
 55  
      */
 56  
     public AntPropertyHelper( MavenProject project, Log l )
 57  0
     {
 58  0
         mavenProject = project;
 59  0
         log = l;
 60  0
     }
 61  
 
 62  
     /**
 63  
      * @deprecated use {@link AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.* properties
 64  
      * @param exprEvaluator
 65  
      * @param l
 66  
      */
 67  
     public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
 68  
     {
 69  0
         this( exprEvaluator, Collections.EMPTY_SET, l );
 70  0
     }
 71  
 
 72  
     /**
 73  
      * @param exprEvaluator
 74  
      * @param artifacts
 75  
      * @param l
 76  
      */
 77  
     public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set artifacts, Log l )
 78  3
     {
 79  3
         this.mavenProject = null;
 80  3
         this.exprEvaluator = exprEvaluator;
 81  3
         this.log = l;
 82  
 
 83  3
         for ( Iterator it = artifacts.iterator(); it.hasNext(); )
 84  
         {
 85  3
             Artifact artifact = (Artifact) it.next();
 86  
 
 87  3
             String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
 88  
                 + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
 89  
                 + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
 90  
 
 91  3
             log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
 92  
 
 93  3
             artifactMap.put( key, artifact.getFile().getPath() );
 94  3
         }
 95  3
     }
 96  
 
 97  
     /**
 98  
      * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)
 99  
      */
 100  
     public synchronized Object getPropertyHook( String ns, String name, boolean user )
 101  
     {
 102  4
         if ( log.isDebugEnabled() )
 103  
         {
 104  0
             log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" );
 105  
         }
 106  
 
 107  
         /* keep old behaviour */
 108  4
         if ( mavenProject != null )
 109  
         {
 110  0
             return getPropertyHook( ns, name, user, mavenProject );
 111  
         }
 112  
 
 113  
 
 114  4
         Object val = null;
 115  
 
 116  4
         if ( name.startsWith( DEPENDENCY_PREFIX ) )
 117  
         {
 118  0
             val = (String) artifactMap.get( name );
 119  
         }
 120  
 
 121  4
         if ( val == null )
 122  
         {
 123  
             try
 124  
             {
 125  4
                 val = exprEvaluator.evaluate( "${" + name + "}" );
 126  
             }
 127  0
             catch ( ExpressionEvaluationException e )
 128  
             {
 129  0
                 if ( log.isErrorEnabled() )
 130  
                 {
 131  0
                     log.error( "Failed to evaluate expression", e );
 132  
                 }
 133  4
             }
 134  
         }
 135  
 
 136  4
         if ( val == null )
 137  
         {
 138  3
             val = super.getPropertyHook( ns, name, user );
 139  
 
 140  3
             if ( val == null )
 141  
             {
 142  3
                 val = System.getProperty( name.toString() );
 143  
             }
 144  
         }
 145  
 
 146  4
         return val;
 147  
     }
 148  
 
 149  
     /**
 150  
      * @deprecated added to keep backwards compatibility
 151  
      * @param ns
 152  
      * @param name
 153  
      * @param user
 154  
      * @param mavenProject
 155  
      * @return
 156  
      */
 157  
     private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject )
 158  
     {
 159  0
         Object val = null;
 160  
         try
 161  
         {
 162  0
             if ( name.startsWith( DEPENDENCY_PREFIX ) )
 163  
             {
 164  0
                 val = (String) artifactMap.get( name );
 165  
             }
 166  0
             else if ( name.startsWith( "project." ) )
 167  
             {
 168  0
                 val = ReflectionValueExtractor.evaluate(
 169  
                     name,
 170  
                     mavenProject,
 171  
                     true
 172  
                 );
 173  
             }
 174  0
             else if ( name.equals( "basedir" ) )
 175  
             {
 176  0
                 val = ReflectionValueExtractor.evaluate(
 177  
                     "basedir.path",
 178  
                     mavenProject,
 179  
                     false
 180  
                 );
 181  
             }
 182  
         }
 183  0
         catch ( Exception e )
 184  
         {
 185  0
             if ( log.isWarnEnabled() )
 186  
             {
 187  0
                 log.warn( "Error evaluating expression '" + name + "'", e );
 188  
             }
 189  0
         }
 190  
 
 191  0
         if ( val == null )
 192  
         {
 193  0
             val = super.getPropertyHook( ns, name, user );
 194  0
             if ( val == null )
 195  
             {
 196  0
                 val = System.getProperty( name.toString() );
 197  
             }
 198  
         }
 199  
 
 200  0
         if ( val instanceof File )
 201  
         {
 202  0
             val = ( (File) val ).getAbsoluteFile();
 203  
         }
 204  
 
 205  0
         return val;
 206  
     }
 207  
 }