1 package org.apache.maven.archiva.repository.project.writers;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
57 * ProjectModel400Writer for Maven 2 project model v4.0.0 pom files.
59 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
63 * role="org.apache.maven.archiva.repository.project.ProjectModelWriter"
64 * role-hint="model400"
66 public class ProjectModel400Writer
67 implements ProjectModelWriter
69 private static final Namespace DEFAULT_NAMESPACE = Namespace.get( "", "http://maven.apache.org/POM/4.0.0" );
71 public void write( ArchivaProjectModel model, File pomFile )
72 throws ProjectModelException, IOException
74 FileWriter writer = null;
77 writer = new FileWriter( pomFile );
78 write( model, writer );
83 IOUtils.closeQuietly( writer );
87 public void write( ArchivaProjectModel model, Writer writer )
88 throws ProjectModelException, IOException
90 Document doc = DocumentHelper.createDocument();
92 Element root = DocumentHelper.createElement( "project" );
94 root.add( DEFAULT_NAMESPACE );
95 root.addNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
96 root.addAttribute( "xsi:schemaLocation",
97 "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" );
99 doc.setRootElement( root );
101 root.addElement( "modelVersion" ).setText( "4.0.0" );
103 addParent( root, model.getParentProject() );
105 addChildElement( root, "groupId", model.getGroupId() );
106 root.addElement( "artifactId" ).setText( model.getArtifactId() );
108 addChildElement( root, "version", model.getVersion() );
110 addChildElement( root, "packaging", model.getPackaging() );
111 addChildElement( root, "name", model.getName() );
112 addChildElement( root, "description", model.getDescription() );
113 addChildElement( root, "url", model.getUrl() );
114 // TODO: add inceptionYear to ArchivaProjectModel
116 addOrganization( root, model.getOrganization() );
118 addIssueManagement( root, model.getIssueManagement() );
119 addCiManagement( root, model.getCiManagement() );
120 addMailingLists( root, model.getMailingLists() );
121 addDevelopersAndContributors( root, model.getIndividuals() );
122 // TODO: add distribution management to ArchivaProjectModel
124 addLicenses( root, model.getLicenses() );
125 addRepositories( root, model.getRepositories() );
126 addDependencyManagement( root, model.getDependencyManagement() );
127 addDependencies( root, model.getDependencies() );
129 addReporting( root, model.getReports() );
130 addScm( root, model.getScm() );
133 addPlugins( root, model.getPlugins() );
134 addBuildExtensions( root, model.getBuildExtensions() );
136 // <distributionManagement>
137 addRelocation( root, model.getRelocation() );
139 fixDefaultNamespace( root );
143 XMLWriter.write( doc, writer );
145 catch ( XMLException e )
147 throw new ProjectModelException( "Unable to write xml contents to writer: " + e.getMessage(), e );
151 private void addArtifactReference( Element elem, ArtifactReference ref, String defaultType )
153 addChildElement( elem, "groupId", ref.getGroupId() );
154 addChildElement( elem, "artifactId", ref.getArtifactId() );
155 addChildElement( elem, "version", ref.getVersion() );
156 addChildElement( elem, "classifier", ref.getClassifier() );
158 if ( !StringUtils.equals( defaultType, ref.getType() ) )
160 addChildElement( elem, "type", ref.getType() );
164 private void addBuildExtensions( Element root, List<ArtifactReference> buildExtensions )
166 if ( CollectionUtils.isEmpty( buildExtensions ) )
171 Element build = root.element( "build" );
174 build = root.addElement( "build" );
177 Element elemExtensions = build.addElement( "extensions" );
179 for ( ArtifactReference extension : buildExtensions )
181 Element elem = elemExtensions.addElement( "extension" );
183 addArtifactReference( elem, extension, "jar" );
187 private void addCiManagement( Element root, CiManagement ciManagement )
189 if ( ciManagement == null )
194 Element elem = root.addElement( "ciManagement" );
195 addChildElement( elem, "system", ciManagement.getSystem() );
196 addChildElement( elem, "url", ciManagement.getUrl() );
197 // TODO: Add notifiers into ArchivaProjectModel
200 private void addDependencies( Element root, List<Dependency> dependencies )
202 if ( CollectionUtils.isEmpty( dependencies ) )
207 addDependencyList( root, dependencies );
210 private void addDependencyList( Element elemParent, List<Dependency> dependencies )
212 if ( CollectionUtils.isEmpty( dependencies ) )
217 Element elemDeps = elemParent.addElement( "dependencies" );
219 for ( Dependency dep : dependencies )
221 Element elem = elemDeps.addElement( "dependency" );
223 addChildElement( elem, "groupId", dep.getGroupId() );
224 addChildElement( elem, "artifactId", dep.getArtifactId() );
225 addChildElement( elem, "version", dep.getVersion() );
226 addChildElement( elem, "classifier", dep.getClassifier() );
227 addChildElement( elem, "type", dep.getType() );
228 addChildElement( elem, "scope", dep.getScope() );
229 addChildElement( elem, "systemPath", dep.getSystemPath() );
231 addExclusions( elem, dep.getExclusions() );
235 private void addDependencyManagement( Element root, List<Dependency> dependencyManagement )
237 if ( CollectionUtils.isEmpty( dependencyManagement ) )
242 Element elemDepMgmt = root.addElement( "dependencyManagement" );
243 addDependencyList( elemDepMgmt, dependencyManagement );
246 private void addDevelopersAndContributors( Element root, List<Individual> individuals )
248 if ( CollectionUtils.isEmpty( individuals ) )
253 Element developers = null;
254 Element contributors = null;
256 for ( Individual individual : individuals )
258 if ( individual.isCommitor() )
260 if ( developers == null )
262 developers = root.addElement( "developers" );
265 Element developer = developers.addElement( "developer" );
266 addChildElement( developer, "id", individual.getPrincipal() );
267 addIndividual( developer, individual );
271 if ( contributors == null )
273 contributors = root.addElement( "contributors" );
276 Element contributor = contributors.addElement( "contributor" );
277 addIndividual( contributor, individual );
282 private void addExclusions( Element elemParent, List<Exclusion> exclusions )
284 if ( CollectionUtils.isEmpty( exclusions ) )
289 Element elemExclusions = elemParent.addElement( "exclusions" );
291 for ( Exclusion exclusion : exclusions )
293 Element elem = elemExclusions.addElement( "exclusion" );
295 addChildElement( elem, "groupId", exclusion.getGroupId() );
296 addChildElement( elem, "artifactId", exclusion.getArtifactId() );
300 private void addIndividual( Element elem, Individual individual )
302 addChildElement( elem, "name", individual.getName() );
303 addChildElement( elem, "email", individual.getEmail() );
304 addChildElement( elem, "organization", individual.getOrganization() );
305 addChildElement( elem, "organizationUrl", individual.getOrganizationUrl() );
306 addChildElement( elem, "timezone", individual.getTimezone() );
308 if ( CollectionUtils.isNotEmpty( individual.getRoles() ) )
310 Element roles = elem.addElement( "roles" );
311 List<String> roleList = individual.getRoles();
312 for ( String roleName : roleList )
314 addChildElement( roles, "role", roleName );
319 private void addIssueManagement( Element root, IssueManagement issueManagement )
321 if ( issueManagement == null )
326 Element elem = root.addElement( "issueManagement" );
327 addChildElement( elem, "system", issueManagement.getSystem() );
328 addChildElement( elem, "url", issueManagement.getUrl() );
331 private void addLicenses( Element root, List<License> licenses )
333 if ( CollectionUtils.isEmpty( licenses ) )
338 Element elemLicenses = root.addElement( "licenses" );
340 for ( License license : licenses )
342 Element elem = elemLicenses.addElement( "license" );
343 addChildElement( elem, "name", license.getName() );
344 addChildElement( elem, "url", license.getUrl() );
345 // TODO: research if we need <distribution> subelement.
349 private void addMailingLists( Element root, List<MailingList> mailingLists )
351 if ( CollectionUtils.isEmpty( mailingLists ) )
356 Element mlists = root.addElement( "mailingLists" );
358 for ( MailingList mailingList : mailingLists )
360 Element mlist = mlists.addElement( "mailingList" );
361 addChildElement( mlist, "name", mailingList.getName() );
362 addChildElement( mlist, "post", mailingList.getPostAddress() );
363 addChildElement( mlist, "subscribe", mailingList.getSubscribeAddress() );
364 addChildElement( mlist, "unsubscribe", mailingList.getUnsubscribeAddress() );
365 addChildElement( mlist, "archive", mailingList.getMainArchiveUrl() );
367 addOtherArchives( mlist, mailingList.getOtherArchives() );
371 private void addOtherArchives( Element mlist, List<String> otherArchives )
373 if ( CollectionUtils.isEmpty( otherArchives ) )
378 Element elemOtherArchives = mlist.addElement( "otherArchives" );
380 for ( String archive : otherArchives )
382 addChildElement( elemOtherArchives, "otherArchive", archive );
386 private void addOrganization( Element root, Organization organization )
388 if ( organization == null )
393 Element elem = root.addElement( "organization" );
395 addChildElement( elem, "name", organization.getName() );
396 addChildElement( elem, "url", organization.getUrl() );
399 private void addParent( Element root, VersionedReference parentProject )
401 if ( parentProject == null )
406 Element parent = root.addElement( "parent" );
407 parent.addElement( "groupId" ).setText( parentProject.getGroupId() );
408 parent.addElement( "artifactId" ).setText( parentProject.getArtifactId() );
409 parent.addElement( "version" ).setText( parentProject.getVersion() );
412 private void addPlugins( Element root, List<ArtifactReference> plugins )
414 if ( CollectionUtils.isEmpty( plugins ) )
419 Element build = root.element( "build" );
422 build = root.addElement( "build" );
425 Element elemPlugins = build.addElement( "plugins" );
427 for ( ArtifactReference plugin : plugins )
429 Element elem = elemPlugins.addElement( "plugin" );
431 addArtifactReference( elem, plugin, "maven-plugin" );
435 private void addRelocation( Element root, VersionedReference relocation )
437 if ( relocation == null )
442 Element distribManagement = root.element( "distributionManagement" );
444 if ( distribManagement == null )
446 distribManagement = root.addElement( "distributionManagement" );
449 Element elem = distribManagement.addElement( "relocation" );
450 addChildElement( elem, "groupId", relocation.getGroupId() );
451 addChildElement( elem, "artifactId", relocation.getArtifactId() );
452 addChildElement( elem, "version", relocation.getVersion() );
455 private void addReporting( Element root, List<ArtifactReference> reports )
457 if ( CollectionUtils.isEmpty( reports ) )
462 Element reporting = root.addElement( "reporting" );
463 Element plugins = reporting.addElement( "plugins" );
465 for ( ArtifactReference reference : reports )
467 Element plugin = plugins.addElement( "plugin" );
468 addChildElement( plugin, "groupId", reference.getGroupId() );
469 addChildElement( plugin, "artifactId", reference.getArtifactId() );
470 addChildElement( plugin, "version", reference.getVersion() );
474 private void addRepositories( Element root, List<ProjectRepository> repositories )
476 if ( CollectionUtils.isEmpty( repositories ) )
481 Element elemRepos = root.addElement( "repositories" );
482 for ( ProjectRepository repository : repositories )
484 Element elem = elemRepos.addElement( "repository" );
485 addChildElement( elem, "id", repository.getId() );
486 addChildElement( elem, "name", repository.getName() );
487 addChildElement( elem, "url", repository.getUrl() );
489 if ( !StringUtils.equals( "default", repository.getLayout() ) )
491 addChildElement( elem, "layout", repository.getLayout() );
496 private void addScm( Element root, Scm scm )
503 Element elem = root.addElement( "scm" );
505 addChildElement( elem, "connection", scm.getConnection() );
506 addChildElement( elem, "developerConnection", scm.getDeveloperConnection() );
507 addChildElement( elem, "url", scm.getUrl() );
511 * Fix the default namespace on all elements recursively.
513 private void fixDefaultNamespace( Element elem )
515 elem.remove( elem.getNamespace() );
516 elem.setQName( QName.get( elem.getName(), DEFAULT_NAMESPACE, elem.getQualifiedName() ) );
520 Iterator<Node> it = elem.elementIterator();
521 while ( it.hasNext() )
525 switch ( n.getNodeType() )
527 case Node.ELEMENT_NODE:
528 fixDefaultNamespace( (Element) n );
534 private static void addChildElement( Element elem, String elemName, String text )
536 if ( StringUtils.isBlank( text ) )
541 elem.addElement( elemName ).setText( text );