1   package org.apache.maven.script.ant;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.tools.ant.PropertyHelper;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
34  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
35  
36  
37  
38  
39  
40  
41  
42  @Deprecated
43  public class AntPropertyHelper
44      extends PropertyHelper
45  {
46      private static final String DEPENDENCY_PREFIX = "maven.dependency.";
47      private Log log;
48      private ExpressionEvaluator exprEvaluator;
49      private MavenProject mavenProject;
50      private Map<String, String> artifactMap = new HashMap<String, String>();
51  
52      
53  
54  
55  
56  
57      public AntPropertyHelper( MavenProject project, Log l )
58      {
59          mavenProject = project;
60          log = l;
61      }
62  
63      
64  
65  
66  
67  
68  
69      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
70      {
71          this( exprEvaluator, Collections.<Artifact>emptySet(), l );
72      }
73  
74      
75  
76  
77  
78  
79      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l )
80      {
81          this.mavenProject = null;
82          this.exprEvaluator = exprEvaluator;
83          this.log = l;
84  
85          for ( Artifact artifact : artifacts )
86          {
87              String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
88                  + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
89                  + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
90  
91              log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
92  
93              artifactMap.put( key, artifact.getFile().getPath() );
94          }
95      }
96  
97      
98  
99  
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         
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 
151 
152 
153 
154 
155 
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 }