001package org.apache.maven.script.ant; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.Set; 027 028import org.apache.maven.artifact.Artifact; 029import org.apache.maven.plugin.logging.Log; 030import org.apache.maven.project.MavenProject; 031import org.apache.tools.ant.PropertyHelper; 032import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 033import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; 034import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; 035 036/** 037 * Makes the ${expressions} used in Maven available to Ant as properties. 038 * 039 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a> 040 */ 041public class AntPropertyHelper 042 extends PropertyHelper 043{ 044 private static final String DEPENDENCY_PREFIX = "maven.dependency."; 045 private Log log; 046 private ExpressionEvaluator exprEvaluator; 047 private MavenProject mavenProject; 048 private Map<String, String> artifactMap = new HashMap<String, String>(); 049 050 /** 051 * @deprecated use the other constructor 052 * @param project 053 * @param l 054 */ 055 public AntPropertyHelper( MavenProject project, Log l ) 056 { 057 mavenProject = project; 058 log = l; 059 } 060 061 /** 062 * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.* 063 * properties 064 * @param exprEvaluator 065 * @param l 066 */ 067 public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l ) 068 { 069 this( exprEvaluator, Collections.<Artifact>emptySet(), l ); 070 } 071 072 /** 073 * @param exprEvaluator 074 * @param artifacts 075 * @param l 076 */ 077 public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l ) 078 { 079 this.mavenProject = null; 080 this.exprEvaluator = exprEvaluator; 081 this.log = l; 082 083 for ( Artifact artifact : artifacts ) 084 { 085 String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId() 086 + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" ) 087 + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path"; 088 089 log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() ); 090 091 artifactMap.put( key, artifact.getFile().getPath() ); 092 } 093 } 094 095 /** 096 * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean) 097 */ 098 public synchronized Object getPropertyHook( String ns, String name, boolean user ) 099 { 100 if ( log.isDebugEnabled() ) 101 { 102 log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" ); 103 } 104 105 /* keep old behaviour */ 106 if ( mavenProject != null ) 107 { 108 return getPropertyHook( ns, name, user, mavenProject ); 109 } 110 111 112 Object val = null; 113 114 if ( name.startsWith( DEPENDENCY_PREFIX ) ) 115 { 116 val = (String) artifactMap.get( name ); 117 } 118 119 if ( val == null ) 120 { 121 try 122 { 123 val = exprEvaluator.evaluate( "${" + name + "}" ); 124 } 125 catch ( ExpressionEvaluationException e ) 126 { 127 if ( log.isErrorEnabled() ) 128 { 129 log.error( "Failed to evaluate expression", e ); 130 } 131 } 132 } 133 134 if ( val == null ) 135 { 136 val = super.getPropertyHook( ns, name, user ); 137 138 if ( val == null ) 139 { 140 val = System.getProperty( name ); 141 } 142 } 143 144 return val; 145 } 146 147 /** 148 * @deprecated added to keep backwards compatibility 149 * @param ns 150 * @param name 151 * @param user 152 * @param mavenProject 153 * @return The property value. 154 */ 155 private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject ) 156 { 157 Object val = null; 158 try 159 { 160 if ( name.startsWith( DEPENDENCY_PREFIX ) ) 161 { 162 val = (String) artifactMap.get( name ); 163 } 164 else if ( name.startsWith( "project." ) ) 165 { 166 val = ReflectionValueExtractor.evaluate( 167 name, 168 mavenProject, 169 true 170 ); 171 } 172 else if ( name.equals( "basedir" ) ) 173 { 174 val = ReflectionValueExtractor.evaluate( 175 "basedir.path", 176 mavenProject, 177 false 178 ); 179 } 180 } 181 catch ( Exception e ) 182 { 183 if ( log.isWarnEnabled() ) 184 { 185 log.warn( "Error evaluating expression '" + name + "'", e ); 186 } 187 } 188 189 if ( val == null ) 190 { 191 val = super.getPropertyHook( ns, name, user ); 192 if ( val == null ) 193 { 194 val = System.getProperty( name ); 195 } 196 } 197 198 if ( val instanceof File ) 199 { 200 val = ( (File) val ).getAbsoluteFile(); 201 } 202 203 return val; 204 } 205}