View Javadoc

1   package org.apache.maven.artifact.ant;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A POM typedef.
50   *
51   * Also an Ant Task that registers a handler called POMPropertyHelper that intercepts all calls to property value
52   * resolution and replies instead of Ant to properties that start with the id of the pom.
53   *
54   * Example: ${maven.project.artifactId}
55   *
56   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
57   * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
58   * @version $Id: Pom.java 773075 2009-05-08 20:16:17Z pgier $
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       * The property interceptor.
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         // TODO: should this be in execute() too? Would that work when it is used as a type?
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     } // -- String getArtifactId()
193 
194     public Build getBuild()
195     {
196         return getMavenProject().getBuild();
197     } // -- Build getBuild()
198 
199     public CiManagement getCiManagement()
200     {
201         return getMavenProject().getCiManagement();
202     } // -- CiManagement getCiManagement()
203 
204     public List getContributors()
205     {
206         return getMavenProject().getContributors();
207     } // -- List getContributors()
208 
209     public List getDependencies()
210     {
211         return getMavenProject().getDependencies();
212     } // -- List getDependencies()
213 
214     public DependencyManagement getDependencyManagement()
215     {
216         return getMavenProject().getDependencyManagement();
217     } // -- DependencyManagement getDependencyManagement()
218 
219     public String getDescription()
220     {
221         return getMavenProject().getDescription();
222     } // -- String getDescription()
223 
224     public List getDevelopers()
225     {
226         return getMavenProject().getDevelopers();
227     } // -- List getDevelopers()
228 
229     public DistributionManagement getDistributionManagement()
230     {
231         return getMavenProject().getDistributionManagement();
232     } // -- DistributionManagement getDistributionManagement()
233 
234     public String getGroupId()
235     {
236         return getMavenProject().getGroupId();
237     } // -- String getGroupId()
238 
239     public String getInceptionYear()
240     {
241         return getMavenProject().getInceptionYear();
242     } // -- String getInceptionYear()
243 
244     public IssueManagement getIssueManagement()
245     {
246         return getMavenProject().getIssueManagement();
247     } // -- IssueManagement getIssueManagement()
248 
249     public List getLicenses()
250     {
251         return getMavenProject().getLicenses();
252     } // -- List getLicenses()
253 
254     public List getMailingLists()
255     {
256         return getMavenProject().getMailingLists();
257     } // -- List getMailingLists()
258 
259     public String getModelVersion()
260     {
261         return getMavenProject().getModelVersion();
262     } // -- String getModelVersion()
263 
264     public List getModules()
265     {
266         return getMavenProject().getModules();
267     } // -- List getModules()
268 
269     public String getName()
270     {
271         return getMavenProject().getName();
272     } // -- String getName()
273 
274     public Organization getOrganization()
275     {
276         return getMavenProject().getOrganization();
277     } // -- Organization getOrganization()
278 
279     public String getPackaging()
280     {
281         return getMavenProject().getPackaging();
282     } // -- String getPackaging()
283 
284     public List getPluginRepositories()
285     {
286         return getMavenProject().getPluginRepositories();
287     } // -- List getPluginRepositories()
288 
289     public Reporting getReporting()
290     {
291         return getMavenProject().getReporting();
292     } // -- Reports getReports()
293 
294     public List getRepositories()
295     {
296         return getMavenProject().getRepositories();
297     } // -- List getRepositories()
298 
299     public Scm getScm()
300     {
301         return getMavenProject().getScm();
302     } // -- Scm getScm()
303 
304     public String getUrl()
305     {
306         return getMavenProject().getUrl();
307     } // -- String getUrl()
308 
309     public String getVersion()
310     {
311         return getMavenProject().getVersion();
312     } // -- String getVersion()
313 
314     public String getId()
315     {
316         return getMavenProject().getId();
317     }
318 
319     /**
320      * Registers POMPropertyHelper as a property interceptor
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         // Add a reference to this task/type
331         project.addReference( antId, this );
332 
333         // Register the property interceptor
334         PropertyHelper phelper = PropertyHelper.getPropertyHelper( project );
335         helper.setNext( phelper.getNext() );
336         helper.setProject( project );
337         phelper.setNext( helper );
338     }
339 
340     /**
341      * The property interceptor that handles the calls for "pom." properties
342      */
343     private class POMPropertyHelper extends PropertyHelper
344     {
345         /**
346          * The method that gets called by Ant with every request of property
347          */
348         public Object getPropertyHook( String ns, String name, boolean user )
349         {
350             String prefix = antId + ".";
351 
352             if ( !name.startsWith( prefix ) )
353             {
354                 // pass on to next interceptor
355                 return super.getPropertyHook( ns, name, user );
356             }
357             try
358             {
359                 // else handle the property resolution
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      * The repositories defined in the ant "pom" task need to be added manually to the profile manager. Otherwise they
400      * won't be available when resolving the parent pom. MANTTASKS-87
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 }