1 | |
package org.apache.maven.artifact.ant; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
24 | |
import org.apache.maven.model.Build; |
25 | |
import org.apache.maven.model.CiManagement; |
26 | |
import org.apache.maven.model.DependencyManagement; |
27 | |
import org.apache.maven.model.DistributionManagement; |
28 | |
import org.apache.maven.model.IssueManagement; |
29 | |
import org.apache.maven.model.Organization; |
30 | |
import org.apache.maven.model.Reporting; |
31 | |
import org.apache.maven.model.Repository; |
32 | |
import org.apache.maven.model.Scm; |
33 | |
import org.apache.maven.profiles.ProfileManager; |
34 | |
import org.apache.maven.project.MavenProject; |
35 | |
import org.apache.maven.project.MavenProjectBuilder; |
36 | |
import org.apache.maven.project.MavenProjectHelper; |
37 | |
import org.apache.maven.project.ProjectBuildingException; |
38 | |
import org.apache.tools.ant.BuildException; |
39 | |
import org.apache.tools.ant.Project; |
40 | |
import org.apache.tools.ant.PropertyHelper; |
41 | |
import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; |
42 | |
|
43 | |
import java.io.File; |
44 | |
import java.util.ArrayList; |
45 | |
import java.util.Iterator; |
46 | |
import java.util.List; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 1 | public class Pom extends AbstractArtifactWithRepositoryTask |
61 | |
{ |
62 | |
private String refid; |
63 | |
|
64 | |
private String antId; |
65 | |
|
66 | |
private MavenProject mavenProject; |
67 | |
|
68 | |
private File file; |
69 | |
|
70 | 1 | private List profiles = new ArrayList(); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 1 | private final POMPropertyHelper helper = new POMPropertyHelper(); |
76 | |
|
77 | |
public String getRefid() |
78 | |
{ |
79 | 0 | return refid; |
80 | |
} |
81 | |
|
82 | |
public void setRefid( String refid ) |
83 | |
{ |
84 | 0 | this.refid = refid; |
85 | 0 | } |
86 | |
|
87 | |
public void setId( String id ) |
88 | |
{ |
89 | 0 | this.antId = id; |
90 | 0 | } |
91 | |
|
92 | |
protected Pom getInstance() |
93 | |
{ |
94 | 0 | Pom instance = this; |
95 | 0 | if ( refid != null ) |
96 | |
{ |
97 | 0 | instance = (Pom) getProject().getReference( refid ); |
98 | 0 | if ( instance == null ) |
99 | |
{ |
100 | 0 | throw new BuildException( "Invalid reference: '" + refid + "'" ); |
101 | |
} |
102 | |
} |
103 | 0 | return instance; |
104 | |
} |
105 | |
|
106 | |
public void setMavenProject( MavenProject mavenProject ) |
107 | |
{ |
108 | 0 | getInstance().mavenProject = mavenProject; |
109 | 0 | } |
110 | |
|
111 | |
public File getFile() |
112 | |
{ |
113 | 0 | return getInstance().file; |
114 | |
} |
115 | |
|
116 | |
public void setFile( File file ) |
117 | |
{ |
118 | 0 | this.file = file; |
119 | 0 | } |
120 | |
|
121 | |
public List getProfiles() |
122 | |
{ |
123 | 0 | return profiles; |
124 | |
} |
125 | |
|
126 | |
public void addProfile(Profile activeProfile) |
127 | |
{ |
128 | 0 | this.profiles.add(activeProfile); |
129 | 0 | } |
130 | |
|
131 | |
public Artifact getArtifact() |
132 | |
{ |
133 | 0 | return getMavenProject().getArtifact(); |
134 | |
} |
135 | |
|
136 | |
public void attach( AttachedArtifact attached ) |
137 | |
{ |
138 | 0 | MavenProjectHelper helper = (MavenProjectHelper) lookup( MavenProjectHelper.ROLE ); |
139 | 0 | MavenProject project = getMavenProject(); |
140 | 0 | if ( attached.getClassifier() != null ) |
141 | |
{ |
142 | 0 | helper.attachArtifact( project, attached.getType(), attached.getClassifier(), attached.getFile() ); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | helper.attachArtifact( project, attached.getType(), attached.getFile() ); |
147 | |
} |
148 | 0 | } |
149 | |
|
150 | |
public List getAttachedArtifacts() |
151 | |
{ |
152 | 0 | return getMavenProject().getAttachedArtifacts(); |
153 | |
} |
154 | |
|
155 | |
void initialise( MavenProjectBuilder builder, ArtifactRepository localRepository ) |
156 | |
{ |
157 | 0 | if ( mavenProject != null ) |
158 | |
{ |
159 | 0 | log( "POM is already initialized for: " + mavenProject.getId(), Project.MSG_DEBUG ); |
160 | |
|
161 | 0 | return; |
162 | |
} |
163 | |
|
164 | 0 | if ( file != null ) |
165 | |
{ |
166 | 0 | addAntRepositoriesToProfileManager(); |
167 | |
|
168 | |
try |
169 | |
{ |
170 | |
|
171 | 0 | mavenProject = builder.build( file, localRepository, getActivatedProfiles() ); |
172 | |
} |
173 | 0 | catch ( ProjectBuildingException e ) |
174 | |
{ |
175 | 0 | throw new BuildException( "Unable to initialize POM " + file.getName() + ": " + e.getMessage(), e ); |
176 | 0 | } |
177 | |
} |
178 | 0 | else if ( refid != null ) |
179 | |
{ |
180 | 0 | getInstance().initialise( builder, localRepository ); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
protected MavenProject getMavenProject() |
185 | |
{ |
186 | 0 | return getInstance().mavenProject; |
187 | |
} |
188 | |
|
189 | |
public String getArtifactId() |
190 | |
{ |
191 | 0 | return getMavenProject().getArtifactId(); |
192 | |
} |
193 | |
|
194 | |
public Build getBuild() |
195 | |
{ |
196 | 0 | return getMavenProject().getBuild(); |
197 | |
} |
198 | |
|
199 | |
public CiManagement getCiManagement() |
200 | |
{ |
201 | 0 | return getMavenProject().getCiManagement(); |
202 | |
} |
203 | |
|
204 | |
public List getContributors() |
205 | |
{ |
206 | 0 | return getMavenProject().getContributors(); |
207 | |
} |
208 | |
|
209 | |
public List getDependencies() |
210 | |
{ |
211 | 0 | return getMavenProject().getDependencies(); |
212 | |
} |
213 | |
|
214 | |
public DependencyManagement getDependencyManagement() |
215 | |
{ |
216 | 0 | return getMavenProject().getDependencyManagement(); |
217 | |
} |
218 | |
|
219 | |
public String getDescription() |
220 | |
{ |
221 | 0 | return getMavenProject().getDescription(); |
222 | |
} |
223 | |
|
224 | |
public List getDevelopers() |
225 | |
{ |
226 | 0 | return getMavenProject().getDevelopers(); |
227 | |
} |
228 | |
|
229 | |
public DistributionManagement getDistributionManagement() |
230 | |
{ |
231 | 0 | return getMavenProject().getDistributionManagement(); |
232 | |
} |
233 | |
|
234 | |
public String getGroupId() |
235 | |
{ |
236 | 0 | return getMavenProject().getGroupId(); |
237 | |
} |
238 | |
|
239 | |
public String getInceptionYear() |
240 | |
{ |
241 | 0 | return getMavenProject().getInceptionYear(); |
242 | |
} |
243 | |
|
244 | |
public IssueManagement getIssueManagement() |
245 | |
{ |
246 | 0 | return getMavenProject().getIssueManagement(); |
247 | |
} |
248 | |
|
249 | |
public List getLicenses() |
250 | |
{ |
251 | 0 | return getMavenProject().getLicenses(); |
252 | |
} |
253 | |
|
254 | |
public List getMailingLists() |
255 | |
{ |
256 | 0 | return getMavenProject().getMailingLists(); |
257 | |
} |
258 | |
|
259 | |
public String getModelVersion() |
260 | |
{ |
261 | 0 | return getMavenProject().getModelVersion(); |
262 | |
} |
263 | |
|
264 | |
public List getModules() |
265 | |
{ |
266 | 0 | return getMavenProject().getModules(); |
267 | |
} |
268 | |
|
269 | |
public String getName() |
270 | |
{ |
271 | 0 | return getMavenProject().getName(); |
272 | |
} |
273 | |
|
274 | |
public Organization getOrganization() |
275 | |
{ |
276 | 0 | return getMavenProject().getOrganization(); |
277 | |
} |
278 | |
|
279 | |
public String getPackaging() |
280 | |
{ |
281 | 0 | return getMavenProject().getPackaging(); |
282 | |
} |
283 | |
|
284 | |
public List getPluginRepositories() |
285 | |
{ |
286 | 0 | return getMavenProject().getPluginRepositories(); |
287 | |
} |
288 | |
|
289 | |
public Reporting getReporting() |
290 | |
{ |
291 | 0 | return getMavenProject().getReporting(); |
292 | |
} |
293 | |
|
294 | |
public List getRepositories() |
295 | |
{ |
296 | 0 | return getMavenProject().getRepositories(); |
297 | |
} |
298 | |
|
299 | |
public Scm getScm() |
300 | |
{ |
301 | 0 | return getMavenProject().getScm(); |
302 | |
} |
303 | |
|
304 | |
public String getUrl() |
305 | |
{ |
306 | 0 | return getMavenProject().getUrl(); |
307 | |
} |
308 | |
|
309 | |
public String getVersion() |
310 | |
{ |
311 | 0 | return getMavenProject().getVersion(); |
312 | |
} |
313 | |
|
314 | |
public String getId() |
315 | |
{ |
316 | 0 | return getMavenProject().getId(); |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
protected void doExecute() |
323 | |
{ |
324 | 0 | ArtifactRepository localRepo = createLocalArtifactRepository(); |
325 | 0 | MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); |
326 | 0 | initialise( projectBuilder, localRepo ); |
327 | |
|
328 | 0 | Project project = getProject(); |
329 | |
|
330 | |
|
331 | 0 | project.addReference( antId, this ); |
332 | |
|
333 | |
|
334 | 0 | PropertyHelper phelper = PropertyHelper.getPropertyHelper( project ); |
335 | 0 | helper.setNext( phelper.getNext() ); |
336 | 0 | helper.setProject( project ); |
337 | 0 | phelper.setNext( helper ); |
338 | 0 | } |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | 2 | private class POMPropertyHelper extends PropertyHelper |
344 | |
{ |
345 | |
|
346 | |
|
347 | |
|
348 | |
public Object getPropertyHook( String ns, String name, boolean user ) |
349 | |
{ |
350 | 0 | String prefix = antId + "."; |
351 | |
|
352 | 0 | if ( !name.startsWith( prefix ) ) |
353 | |
{ |
354 | |
|
355 | 0 | return super.getPropertyHook( ns, name, user ); |
356 | |
} |
357 | |
try |
358 | |
{ |
359 | |
|
360 | 0 | String expression = name.substring( prefix.length() ); |
361 | 0 | return getPOMValue( "project." + expression ); |
362 | |
} |
363 | 0 | catch ( Exception ex ) |
364 | |
{ |
365 | 0 | ex.printStackTrace(); |
366 | 0 | return null; |
367 | |
} |
368 | |
} |
369 | |
|
370 | |
private static final String PROPERTIES_PREFIX = "project.properties."; |
371 | |
|
372 | |
private Object getPOMValue( String expression ) |
373 | |
{ |
374 | 0 | Object value = null; |
375 | |
|
376 | |
try |
377 | |
{ |
378 | 0 | if ( expression.startsWith( PROPERTIES_PREFIX ) ) |
379 | |
{ |
380 | 0 | expression = expression.substring( PROPERTIES_PREFIX.length() ); |
381 | 0 | value = getMavenProject().getProperties().get( expression ); |
382 | |
} |
383 | |
else |
384 | |
{ |
385 | 0 | value = ReflectionValueExtractor.evaluate( expression, getMavenProject() ); |
386 | |
} |
387 | |
} |
388 | 0 | catch ( Exception e ) |
389 | |
{ |
390 | 0 | throw new BuildException( "Error extracting expression from POM", e ); |
391 | 0 | } |
392 | |
|
393 | 0 | return value; |
394 | |
} |
395 | |
|
396 | |
} |
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
private void addAntRepositoriesToProfileManager() |
403 | |
{ |
404 | 0 | List remoteRepositories = this.getRemoteRepositories(); |
405 | |
|
406 | 0 | if ( remoteRepositories == null || remoteRepositories.isEmpty() ) |
407 | |
{ |
408 | 0 | return; |
409 | |
} |
410 | 0 | org.apache.maven.model.Profile repositoriesProfile = new org.apache.maven.model.Profile(); |
411 | 0 | repositoriesProfile.setId( "maven-ant-tasks-repo-profile" ); |
412 | |
|
413 | 0 | Iterator iter = remoteRepositories.iterator(); |
414 | 0 | while ( iter.hasNext() ) |
415 | |
{ |
416 | 0 | RemoteRepository antRepo = (RemoteRepository) iter.next(); |
417 | 0 | Repository mavenRepo = new Repository(); |
418 | 0 | mavenRepo.setId( antRepo.getId() ); |
419 | 0 | mavenRepo.setUrl( antRepo.getUrl() ); |
420 | 0 | repositoriesProfile.addRepository( mavenRepo ); |
421 | 0 | } |
422 | |
|
423 | 0 | getProfileManager().addProfile( repositoriesProfile ); |
424 | 0 | getProfileManager().explicitlyActivate( repositoriesProfile.getId() ); |
425 | 0 | } |
426 | |
|
427 | |
private ProfileManager getActivatedProfiles() |
428 | |
{ |
429 | 0 | ProfileManager profileManager = getProfileManager(); |
430 | |
|
431 | 0 | Iterator it = getProfiles().iterator(); |
432 | 0 | while ( it.hasNext() ) |
433 | |
{ |
434 | 0 | Profile profile = (Profile) it.next(); |
435 | |
|
436 | 0 | if ( profile.getId() == null ) |
437 | |
{ |
438 | 0 | throw new BuildException( "Attribute \"id\" is required for profile in pom type." ); |
439 | |
} |
440 | |
|
441 | 0 | if ( profile.getActive() == null || Boolean.valueOf( profile.getActive() ).booleanValue() ) |
442 | |
{ |
443 | 0 | profileManager.explicitlyActivate( profile.getId() ); |
444 | |
} |
445 | |
else |
446 | |
{ |
447 | 0 | profileManager.explicitlyDeactivate( profile.getId() ); |
448 | |
} |
449 | |
|
450 | 0 | } |
451 | 0 | return profileManager; |
452 | |
} |
453 | |
} |