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 * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
041 */
042@Deprecated
043public class AntPropertyHelper
044    extends PropertyHelper
045{
046    private static final String DEPENDENCY_PREFIX = "maven.dependency.";
047    private Log log;
048    private ExpressionEvaluator exprEvaluator;
049    private MavenProject mavenProject;
050    private Map<String, String> artifactMap = new HashMap<String, String>();
051
052    /**
053     * @deprecated use the other constructor
054     * @param project
055     * @param l
056     */
057    public AntPropertyHelper( MavenProject project, Log l )
058    {
059        mavenProject = project;
060        log = l;
061    }
062
063    /**
064     * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.*
065     * properties
066     * @param exprEvaluator
067     * @param l
068     */
069    public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
070    {
071        this( exprEvaluator, Collections.<Artifact>emptySet(), l );
072    }
073
074    /**
075     * @param exprEvaluator
076     * @param artifacts
077     * @param l
078     */
079    public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l )
080    {
081        this.mavenProject = null;
082        this.exprEvaluator = exprEvaluator;
083        this.log = l;
084
085        for ( Artifact artifact : artifacts )
086        {
087            String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
088                + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
089                + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
090
091            log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
092
093            artifactMap.put( key, artifact.getFile().getPath() );
094        }
095    }
096
097    /**
098     * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)
099     */
100    public synchronized Object getPropertyHook( String ns, String name, boolean user )
101    {
102        if ( log.isDebugEnabled() )
103        {
104            log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" );
105        }
106
107        /* keep old behaviour */
108        if ( mavenProject != null )
109        {
110            return getPropertyHook( ns, name, user, mavenProject );
111        }
112
113
114        Object val = null;
115
116        if ( name.startsWith( DEPENDENCY_PREFIX ) )
117        {
118            val = artifactMap.get( name );
119        }
120
121        if ( val == null )
122        {
123            try
124            {
125                val = exprEvaluator.evaluate( "${" + name + "}" );
126            }
127            catch ( ExpressionEvaluationException e )
128            {
129                if ( log.isErrorEnabled() )
130                {
131                    log.error( "Failed to evaluate expression", e );
132                }
133            }
134        }
135
136        if ( val == null )
137        {
138            val = super.getPropertyHook( ns, name, user );
139
140            if ( val == null )
141            {
142                val = System.getProperty( name );
143            }
144        }
145
146        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 The property value.
156     */
157    private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject )
158    {
159        Object val = null;
160        try
161        {
162            if ( name.startsWith( DEPENDENCY_PREFIX ) )
163            {
164                val = artifactMap.get( name );
165            }
166            else if ( name.startsWith( "project." ) )
167            {
168                val = ReflectionValueExtractor.evaluate(
169                    name,
170                    mavenProject,
171                    true
172                );
173            }
174            else if ( name.equals( "basedir" ) )
175            {
176                val = ReflectionValueExtractor.evaluate(
177                    "basedir.path",
178                    mavenProject,
179                    false
180                );
181            }
182        }
183        catch ( Exception e )
184        {
185            if ( log.isWarnEnabled() )
186            {
187                log.warn( "Error evaluating expression '" + name + "'", e );
188            }
189        }
190
191        if ( val == null )
192        {
193            val = super.getPropertyHook( ns, name, user );
194            if ( val == null )
195            {
196                val = System.getProperty( name );
197            }
198        }
199
200        if ( val instanceof File )
201        {
202            val = ( (File) val ).getAbsoluteFile();
203        }
204
205        return val;
206    }
207}