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