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.Hashtable;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.plugin.logging.Log;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.tools.ant.PropertyHelper;
33 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
34 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
35 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
36
37
38
39
40
41
42 public class AntPropertyHelper
43 extends PropertyHelper
44 {
45 private static final String DEPENDENCY_PREFIX = "maven.dependency.";
46 private Log log;
47 private ExpressionEvaluator exprEvaluator;
48 private MavenProject mavenProject;
49 private Map artifactMap = new Hashtable();
50
51
52
53
54
55
56 public AntPropertyHelper( MavenProject project, Log l )
57 {
58 mavenProject = project;
59 log = l;
60 }
61
62
63
64
65
66
67 public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
68 {
69 this( exprEvaluator, Collections.EMPTY_SET, l );
70 }
71
72
73
74
75
76
77 public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set artifacts, Log l )
78 {
79 this.mavenProject = null;
80 this.exprEvaluator = exprEvaluator;
81 this.log = l;
82
83 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
84 {
85 Artifact artifact = (Artifact) it.next();
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 = (String) 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.toString() );
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 = (String) 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.toString() );
197 }
198 }
199
200 if ( val instanceof File )
201 {
202 val = ( (File) val ).getAbsoluteFile();
203 }
204
205 return val;
206 }
207 }