1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
37  
38  
39  
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  
51  
52  
53  
54      public AntPropertyHelper(MavenProject project, Log l) {
55          mavenProject = project;
56          log = l;
57      }
58  
59      
60  
61  
62  
63  
64  
65      public AntPropertyHelper(ExpressionEvaluator exprEvaluator, Log l) {
66          this(exprEvaluator, Collections.<Artifact>emptySet(), l);
67      }
68  
69      
70  
71  
72  
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  
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          
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 
132 
133 
134 
135 
136 
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 }