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