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  public class AntPropertyHelper
42      extends PropertyHelper
43  {
44      private static final String DEPENDENCY_PREFIX = "maven.dependency.";
45      private Log log;
46      private ExpressionEvaluator exprEvaluator;
47      private MavenProject mavenProject;
48      private Map<String, String> artifactMap = new HashMap<String, String>();
49  
50      
51  
52  
53  
54  
55      public AntPropertyHelper( MavenProject project, Log l )
56      {
57          mavenProject = project;
58          log = l;
59      }
60  
61      
62  
63  
64  
65  
66  
67      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
68      {
69          this( exprEvaluator, Collections.<Artifact>emptySet(), l );
70      }
71  
72      
73  
74  
75  
76  
77      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l )
78      {
79          this.mavenProject = null;
80          this.exprEvaluator = exprEvaluator;
81          this.log = l;
82  
83          for ( Artifact artifact : artifacts )
84          {
85              String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
86                  + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
87                  + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
88  
89              log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
90  
91              artifactMap.put( key, artifact.getFile().getPath() );
92          }
93      }
94  
95      
96  
97  
98      public synchronized Object getPropertyHook( String ns, String name, boolean user )
99      {
100         if ( log.isDebugEnabled() )
101         {
102             log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" );
103         }
104 
105         
106         if ( mavenProject != null )
107         {
108             return getPropertyHook( ns, name, user, mavenProject );
109         }
110 
111 
112         Object val = null;
113 
114         if ( name.startsWith( DEPENDENCY_PREFIX ) )
115         {
116             val = (String) artifactMap.get( name );
117         }
118 
119         if ( val == null )
120         {
121             try
122             {
123                 val = exprEvaluator.evaluate( "${" + name + "}" );
124             }
125             catch ( ExpressionEvaluationException e )
126             {
127                 if ( log.isErrorEnabled() )
128                 {
129                     log.error( "Failed to evaluate expression", e );
130                 }
131             }
132         }
133 
134         if ( val == null )
135         {
136             val = super.getPropertyHook( ns, name, user );
137 
138             if ( val == null )
139             {
140                 val = System.getProperty( name );
141             }
142         }
143 
144         return val;
145     }
146 
147     
148 
149 
150 
151 
152 
153 
154 
155     private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject )
156     {
157         Object val = null;
158         try
159         {
160             if ( name.startsWith( DEPENDENCY_PREFIX ) )
161             {
162                 val = (String) artifactMap.get( name );
163             }
164             else if ( name.startsWith( "project." ) )
165             {
166                 val = ReflectionValueExtractor.evaluate(
167                     name,
168                     mavenProject,
169                     true
170                 );
171             }
172             else if ( name.equals( "basedir" ) )
173             {
174                 val = ReflectionValueExtractor.evaluate(
175                     "basedir.path",
176                     mavenProject,
177                     false
178                 );
179             }
180         }
181         catch ( Exception e )
182         {
183             if ( log.isWarnEnabled() )
184             {
185                 log.warn( "Error evaluating expression '" + name + "'", e );
186             }
187         }
188 
189         if ( val == null )
190         {
191             val = super.getPropertyHook( ns, name, user );
192             if ( val == null )
193             {
194                 val = System.getProperty( name );
195             }
196         }
197 
198         if ( val instanceof File )
199         {
200             val = ( (File) val ).getAbsoluteFile();
201         }
202 
203         return val;
204     }
205 }