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