View Javadoc

1   package org.apache.maven.archiva.repository.project.writers;
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.commons.collections.CollectionUtils;
23  import org.apache.commons.io.IOUtils;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.maven.archiva.model.ArchivaProjectModel;
26  import org.apache.maven.archiva.model.ArtifactReference;
27  import org.apache.maven.archiva.model.CiManagement;
28  import org.apache.maven.archiva.model.Dependency;
29  import org.apache.maven.archiva.model.Exclusion;
30  import org.apache.maven.archiva.model.Individual;
31  import org.apache.maven.archiva.model.IssueManagement;
32  import org.apache.maven.archiva.model.License;
33  import org.apache.maven.archiva.model.MailingList;
34  import org.apache.maven.archiva.model.Organization;
35  import org.apache.maven.archiva.model.ProjectRepository;
36  import org.apache.maven.archiva.model.Scm;
37  import org.apache.maven.archiva.model.VersionedReference;
38  import org.apache.maven.archiva.repository.project.ProjectModelException;
39  import org.apache.maven.archiva.repository.project.ProjectModelWriter;
40  import org.apache.maven.archiva.xml.XMLException;
41  import org.apache.maven.archiva.xml.XMLWriter;
42  import org.dom4j.Document;
43  import org.dom4j.DocumentHelper;
44  import org.dom4j.Element;
45  import org.dom4j.Namespace;
46  import org.dom4j.Node;
47  import org.dom4j.QName;
48  
49  import java.io.File;
50  import java.io.FileWriter;
51  import java.io.IOException;
52  import java.io.Writer;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * ProjectModel400Writer for Maven 2 project model v4.0.0 pom files.  
58   *
59   * @version $Id: ProjectModel400Writer.java 778118 2009-05-24 10:58:49Z oching $
60   */
61  public class ProjectModel400Writer
62      implements ProjectModelWriter
63  {
64      private static final Namespace DEFAULT_NAMESPACE = Namespace.get( "", "http://maven.apache.org/POM/4.0.0" );
65  
66      public void write( ArchivaProjectModel model, File pomFile )
67          throws ProjectModelException, IOException
68      {
69          FileWriter writer = null;
70          try
71          {
72              writer = new FileWriter( pomFile );
73              write( model, writer );
74              writer.flush();
75          }
76          finally
77          {
78              IOUtils.closeQuietly( writer );
79          }
80      }
81  
82      public void write( ArchivaProjectModel model, Writer writer )
83          throws ProjectModelException, IOException
84      {
85          Document doc = DocumentHelper.createDocument();
86  
87          Element root = DocumentHelper.createElement( "project" );
88  
89          root.add( DEFAULT_NAMESPACE );
90          root.addNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
91          root.addAttribute( "xsi:schemaLocation",
92                             "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" );
93  
94          doc.setRootElement( root );
95  
96          root.addElement( "modelVersion" ).setText( "4.0.0" );
97  
98          addParent( root, model.getParentProject() );
99  
100         addChildElement( root, "groupId", model.getGroupId() );
101         root.addElement( "artifactId" ).setText( model.getArtifactId() );
102 
103         addChildElement( root, "version", model.getVersion() );
104 
105         addChildElement( root, "packaging", model.getPackaging() );
106         addChildElement( root, "name", model.getName() );
107         addChildElement( root, "description", model.getDescription() );
108         addChildElement( root, "url", model.getUrl() );
109         // TODO: add inceptionYear to ArchivaProjectModel
110 
111         addOrganization( root, model.getOrganization() );
112 
113         addIssueManagement( root, model.getIssueManagement() );
114         addCiManagement( root, model.getCiManagement() );
115         addMailingLists( root, model.getMailingLists() );
116         addDevelopersAndContributors( root, model.getIndividuals() );
117         // TODO: add distribution management to ArchivaProjectModel
118 
119         addLicenses( root, model.getLicenses() );
120         addRepositories( root, model.getRepositories() );
121         addDependencyManagement( root, model.getDependencyManagement() );
122         addDependencies( root, model.getDependencies() );
123 
124         addReporting( root, model.getReports() );
125         addScm( root, model.getScm() );
126 
127         // <build> element
128         addPlugins( root, model.getPlugins() );
129         addBuildExtensions( root, model.getBuildExtensions() );
130 
131         // <distributionManagement>
132         addRelocation( root, model.getRelocation() );
133 
134         fixDefaultNamespace( root );
135 
136         try
137         {
138             XMLWriter.write( doc, writer );
139         }
140         catch ( XMLException e )
141         {
142             throw new ProjectModelException( "Unable to write xml contents to writer: " + e.getMessage(), e );
143         }
144     }
145 
146     private void addArtifactReference( Element elem, ArtifactReference ref, String defaultType )
147     {
148         addChildElement( elem, "groupId", ref.getGroupId() );
149         addChildElement( elem, "artifactId", ref.getArtifactId() );
150         addChildElement( elem, "version", ref.getVersion() );
151         addChildElement( elem, "classifier", ref.getClassifier() );
152 
153         if ( !StringUtils.equals( defaultType, ref.getType() ) )
154         {
155             addChildElement( elem, "type", ref.getType() );
156         }
157     }
158 
159     private void addBuildExtensions( Element root, List<ArtifactReference> buildExtensions )
160     {
161         if ( CollectionUtils.isEmpty( buildExtensions ) )
162         {
163             return;
164         }
165 
166         Element build = root.element( "build" );
167         if ( build == null )
168         {
169             build = root.addElement( "build" );
170         }
171 
172         Element elemExtensions = build.addElement( "extensions" );
173 
174         for ( ArtifactReference extension : buildExtensions )
175         {
176             Element elem = elemExtensions.addElement( "extension" );
177 
178             addArtifactReference( elem, extension, "jar" );
179         }
180     }
181 
182     private void addCiManagement( Element root, CiManagement ciManagement )
183     {
184         if ( ciManagement == null )
185         {
186             return;
187         }
188 
189         Element elem = root.addElement( "ciManagement" );
190         addChildElement( elem, "system", ciManagement.getSystem() );
191         addChildElement( elem, "url", ciManagement.getUrl() );
192         // TODO: Add notifiers into ArchivaProjectModel 
193     }
194 
195     private void addDependencies( Element root, List<Dependency> dependencies )
196     {
197         if ( CollectionUtils.isEmpty( dependencies ) )
198         {
199             return;
200         }
201 
202         addDependencyList( root, dependencies );
203     }
204 
205     private void addDependencyList( Element elemParent, List<Dependency> dependencies )
206     {
207         if ( CollectionUtils.isEmpty( dependencies ) )
208         {
209             return;
210         }
211 
212         Element elemDeps = elemParent.addElement( "dependencies" );
213 
214         for ( Dependency dep : dependencies )
215         {
216             Element elem = elemDeps.addElement( "dependency" );
217 
218             addChildElement( elem, "groupId", dep.getGroupId() );
219             addChildElement( elem, "artifactId", dep.getArtifactId() );
220             addChildElement( elem, "version", dep.getVersion() );
221             addChildElement( elem, "classifier", dep.getClassifier() );
222             addChildElement( elem, "type", dep.getType() );
223             addChildElement( elem, "scope", dep.getScope() );
224             addChildElement( elem, "systemPath", dep.getSystemPath() );
225 
226             addExclusions( elem, dep.getExclusions() );
227         }
228     }
229 
230     private void addDependencyManagement( Element root, List<Dependency> dependencyManagement )
231     {
232         if ( CollectionUtils.isEmpty( dependencyManagement ) )
233         {
234             return;
235         }
236 
237         Element elemDepMgmt = root.addElement( "dependencyManagement" );
238         addDependencyList( elemDepMgmt, dependencyManagement );
239     }
240 
241     private void addDevelopersAndContributors( Element root, List<Individual> individuals )
242     {
243         if ( CollectionUtils.isEmpty( individuals ) )
244         {
245             return;
246         }
247 
248         Element developers = null;
249         Element contributors = null;
250 
251         for ( Individual individual : individuals )
252         {
253             if ( individual.isCommitor() )
254             {
255                 if ( developers == null )
256                 {
257                     developers = root.addElement( "developers" );
258                 }
259 
260                 Element developer = developers.addElement( "developer" );
261                 addChildElement( developer, "id", individual.getPrincipal() );
262                 addIndividual( developer, individual );
263             }
264             else
265             {
266                 if ( contributors == null )
267                 {
268                     contributors = root.addElement( "contributors" );
269                 }
270 
271                 Element contributor = contributors.addElement( "contributor" );
272                 addIndividual( contributor, individual );
273             }
274         }
275     }
276 
277     private void addExclusions( Element elemParent, List<Exclusion> exclusions )
278     {
279         if ( CollectionUtils.isEmpty( exclusions ) )
280         {
281             return;
282         }
283 
284         Element elemExclusions = elemParent.addElement( "exclusions" );
285 
286         for ( Exclusion exclusion : exclusions )
287         {
288             Element elem = elemExclusions.addElement( "exclusion" );
289 
290             addChildElement( elem, "groupId", exclusion.getGroupId() );
291             addChildElement( elem, "artifactId", exclusion.getArtifactId() );
292         }
293     }
294 
295     private void addIndividual( Element elem, Individual individual )
296     {
297         addChildElement( elem, "name", individual.getName() );
298         addChildElement( elem, "email", individual.getEmail() );
299         addChildElement( elem, "organization", individual.getOrganization() );
300         addChildElement( elem, "organizationUrl", individual.getOrganizationUrl() );
301         addChildElement( elem, "timezone", individual.getTimezone() );
302 
303         if ( CollectionUtils.isNotEmpty( individual.getRoles() ) )
304         {
305             Element roles = elem.addElement( "roles" );
306             List<String> roleList = individual.getRoles();
307             for ( String roleName : roleList )
308             {
309                 addChildElement( roles, "role", roleName );
310             }
311         }
312     }
313 
314     private void addIssueManagement( Element root, IssueManagement issueManagement )
315     {
316         if ( issueManagement == null )
317         {
318             return;
319         }
320 
321         Element elem = root.addElement( "issueManagement" );
322         addChildElement( elem, "system", issueManagement.getSystem() );
323         addChildElement( elem, "url", issueManagement.getUrl() );
324     }
325 
326     private void addLicenses( Element root, List<License> licenses )
327     {
328         if ( CollectionUtils.isEmpty( licenses ) )
329         {
330             return;
331         }
332 
333         Element elemLicenses = root.addElement( "licenses" );
334 
335         for ( License license : licenses )
336         {
337             Element elem = elemLicenses.addElement( "license" );
338             addChildElement( elem, "name", license.getName() );
339             addChildElement( elem, "url", license.getUrl() );
340             // TODO: research if we need <distribution> subelement.
341         }
342     }
343 
344     private void addMailingLists( Element root, List<MailingList> mailingLists )
345     {
346         if ( CollectionUtils.isEmpty( mailingLists ) )
347         {
348             return;
349         }
350 
351         Element mlists = root.addElement( "mailingLists" );
352 
353         for ( MailingList mailingList : mailingLists )
354         {
355             Element mlist = mlists.addElement( "mailingList" );
356             addChildElement( mlist, "name", mailingList.getName() );
357             addChildElement( mlist, "post", mailingList.getPostAddress() );
358             addChildElement( mlist, "subscribe", mailingList.getSubscribeAddress() );
359             addChildElement( mlist, "unsubscribe", mailingList.getUnsubscribeAddress() );
360             addChildElement( mlist, "archive", mailingList.getMainArchiveUrl() );
361 
362             addOtherArchives( mlist, mailingList.getOtherArchives() );
363         }
364     }
365 
366     private void addOtherArchives( Element mlist, List<String> otherArchives )
367     {
368         if ( CollectionUtils.isEmpty( otherArchives ) )
369         {
370             return;
371         }
372 
373         Element elemOtherArchives = mlist.addElement( "otherArchives" );
374 
375         for ( String archive : otherArchives )
376         {
377             addChildElement( elemOtherArchives, "otherArchive", archive );
378         }
379     }
380 
381     private void addOrganization( Element root, Organization organization )
382     {
383         if ( organization == null )
384         {
385             return;
386         }
387 
388         Element elem = root.addElement( "organization" );
389 
390         //addChildElement( elem, "name", organization.getOrganizationName() );
391         addChildElement( elem, "name", organization.getName() );
392         addChildElement( elem, "url", organization.getUrl() );
393     }
394 
395     private void addParent( Element root, VersionedReference parentProject )
396     {
397         if ( parentProject == null )
398         {
399             return;
400         }
401 
402         Element parent = root.addElement( "parent" );
403         parent.addElement( "groupId" ).setText( parentProject.getGroupId() );
404         parent.addElement( "artifactId" ).setText( parentProject.getArtifactId() );
405         parent.addElement( "version" ).setText( parentProject.getVersion() );
406     }
407 
408     private void addPlugins( Element root, List<ArtifactReference> plugins )
409     {
410         if ( CollectionUtils.isEmpty( plugins ) )
411         {
412             return;
413         }
414 
415         Element build = root.element( "build" );
416         if ( build == null )
417         {
418             build = root.addElement( "build" );
419         }
420 
421         Element elemPlugins = build.addElement( "plugins" );
422 
423         for ( ArtifactReference plugin : plugins )
424         {
425             Element elem = elemPlugins.addElement( "plugin" );
426 
427             addArtifactReference( elem, plugin, "maven-plugin" );
428         }
429     }
430 
431     private void addRelocation( Element root, VersionedReference relocation )
432     {
433         if ( relocation == null )
434         {
435             return;
436         }
437 
438         Element distribManagement = root.element( "distributionManagement" );
439 
440         if ( distribManagement == null )
441         {
442             distribManagement = root.addElement( "distributionManagement" );
443         }
444 
445         Element elem = distribManagement.addElement( "relocation" );
446         addChildElement( elem, "groupId", relocation.getGroupId() );
447         addChildElement( elem, "artifactId", relocation.getArtifactId() );
448         addChildElement( elem, "version", relocation.getVersion() );
449     }
450 
451     private void addReporting( Element root, List<ArtifactReference> reports )
452     {
453         if ( CollectionUtils.isEmpty( reports ) )
454         {
455             return;
456         }
457 
458         Element reporting = root.addElement( "reporting" );
459         Element plugins = reporting.addElement( "plugins" );
460 
461         for ( ArtifactReference reference : reports )
462         {
463             Element plugin = plugins.addElement( "plugin" );
464             addChildElement( plugin, "groupId", reference.getGroupId() );
465             addChildElement( plugin, "artifactId", reference.getArtifactId() );
466             addChildElement( plugin, "version", reference.getVersion() );
467         }
468     }
469 
470     private void addRepositories( Element root, List<ProjectRepository> repositories )
471     {
472         if ( CollectionUtils.isEmpty( repositories ) )
473         {
474             return;
475         }
476 
477         Element elemRepos = root.addElement( "repositories" );
478         for ( ProjectRepository repository : repositories )
479         {
480             Element elem = elemRepos.addElement( "repository" );
481             addChildElement( elem, "id", repository.getId() );
482             addChildElement( elem, "name", repository.getName() );
483             addChildElement( elem, "url", repository.getUrl() );
484 
485             if ( !StringUtils.equals( "default", repository.getLayout() ) )
486             {
487                 addChildElement( elem, "layout", repository.getLayout() );
488             }
489         }
490     }
491 
492     private void addScm( Element root, Scm scm )
493     {
494         if ( scm == null )
495         {
496             return;
497         }
498 
499         Element elem = root.addElement( "scm" );
500 
501         addChildElement( elem, "connection", scm.getConnection() );
502         addChildElement( elem, "developerConnection", scm.getDeveloperConnection() );
503         addChildElement( elem, "url", scm.getUrl() );
504     }
505 
506     /**
507      * Fix the default namespace on all elements recursively.
508      */
509     @SuppressWarnings("unchecked")
510     private void fixDefaultNamespace( Element elem )
511     {
512         elem.remove( elem.getNamespace() );
513         elem.setQName( QName.get( elem.getName(), DEFAULT_NAMESPACE, elem.getQualifiedName() ) );
514 
515         Node n;
516 
517         Iterator<Node> it = elem.elementIterator();
518         while ( it.hasNext() )
519         {
520             n = it.next();
521 
522             switch ( n.getNodeType() )
523             {
524                 case Node.ELEMENT_NODE:
525                     fixDefaultNamespace( (Element) n );
526                     break;
527             }
528         }
529     }
530 
531     private static void addChildElement( Element elem, String elemName, String text )
532     {
533         if ( StringUtils.isBlank( text ) )
534         {
535             return;
536         }
537 
538         elem.addElement( elemName ).setText( text );
539     }
540 }