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