View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.script.ant;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.tools.ant.PropertyHelper;
31  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
34  
35  /**
36   * Makes the ${expressions} used in Maven available to Ant as properties.
37   *
38   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
39   * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
40   */
41  @Deprecated
42  public class AntPropertyHelper extends PropertyHelper {
43      private static final String DEPENDENCY_PREFIX = "maven.dependency.";
44      private Log log;
45      private ExpressionEvaluator exprEvaluator;
46      private MavenProject mavenProject;
47      private Map<String, String> artifactMap = new HashMap<>();
48  
49      /**
50       * @deprecated use the other constructor
51       * @param project
52       * @param l
53       */
54      public AntPropertyHelper(MavenProject project, Log l) {
55          mavenProject = project;
56          log = l;
57      }
58  
59      /**
60       * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.*
61       * properties
62       * @param exprEvaluator
63       * @param l
64       */
65      public AntPropertyHelper(ExpressionEvaluator exprEvaluator, Log l) {
66          this(exprEvaluator, Collections.<Artifact>emptySet(), l);
67      }
68  
69      /**
70       * @param exprEvaluator
71       * @param artifacts
72       * @param l
73       */
74      public AntPropertyHelper(ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l) {
75          this.mavenProject = null;
76          this.exprEvaluator = exprEvaluator;
77          this.log = l;
78  
79          for (Artifact artifact : artifacts) {
80              String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
81                      + (artifact.getClassifier() != null ? "." + artifact.getClassifier() : "")
82                      + (artifact.getType() != null ? "." + artifact.getType() : "") + ".path";
83  
84              log.debug("Storing: " + key + "=" + artifact.getFile().getPath());
85  
86              artifactMap.put(key, artifact.getFile().getPath());
87          }
88      }
89  
90      /**
91       * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)
92       */
93      public synchronized Object getPropertyHook(String ns, String name, boolean user) {
94          if (log.isDebugEnabled()) {
95              log.debug("getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")");
96          }
97  
98          /* keep old behaviour */
99          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 }