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.
61 public class ProjectModel400Writer
62 implements ProjectModelWriter
64 private static final Namespace DEFAULT_NAMESPACE = Namespace.get( "", "http://maven.apache.org/POM/4.0.0" );
66 public void write( ArchivaProjectModel model, File pomFile )
67 throws ProjectModelException, IOException
69 FileWriter writer = null;
72 writer = new FileWriter( pomFile );
73 write( model, writer );
78 IOUtils.closeQuietly( writer );
82 public void write( ArchivaProjectModel model, Writer writer )
83 throws ProjectModelException, IOException
85 Document doc = DocumentHelper.createDocument();
87 Element root = DocumentHelper.createElement( "project" );
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" );
94 doc.setRootElement( root );
96 root.addElement( "modelVersion" ).setText( "4.0.0" );
98 addParent( root, model.getParentProject() );
100 addChildElement( root, "groupId", model.getGroupId() );
101 root.addElement( "artifactId" ).setText( model.getArtifactId() );
103 addChildElement( root, "version", model.getVersion() );
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
111 addOrganization( root, model.getOrganization() );
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
119 addLicenses( root, model.getLicenses() );
120 addRepositories( root, model.getRepositories() );
121 addDependencyManagement( root, model.getDependencyManagement() );
122 addDependencies( root, model.getDependencies() );
124 addReporting( root, model.getReports() );
125 addScm( root, model.getScm() );
128 addPlugins( root, model.getPlugins() );
129 addBuildExtensions( root, model.getBuildExtensions() );
131 // <distributionManagement>
132 addRelocation( root, model.getRelocation() );
134 fixDefaultNamespace( root );
138 XMLWriter.write( doc, writer );
140 catch ( XMLException e )
142 throw new ProjectModelException( "Unable to write xml contents to writer: " + e.getMessage(), e );
146 private void addArtifactReference( Element elem, ArtifactReference ref, String defaultType )
148 addChildElement( elem, "groupId", ref.getGroupId() );
149 addChildElement( elem, "artifactId", ref.getArtifactId() );
150 addChildElement( elem, "version", ref.getVersion() );
151 addChildElement( elem, "classifier", ref.getClassifier() );
153 if ( !StringUtils.equals( defaultType, ref.getType() ) )
155 addChildElement( elem, "type", ref.getType() );
159 private void addBuildExtensions( Element root, List<ArtifactReference> buildExtensions )
161 if ( CollectionUtils.isEmpty( buildExtensions ) )
166 Element build = root.element( "build" );
169 build = root.addElement( "build" );
172 Element elemExtensions = build.addElement( "extensions" );
174 for ( ArtifactReference extension : buildExtensions )
176 Element elem = elemExtensions.addElement( "extension" );
178 addArtifactReference( elem, extension, "jar" );
182 private void addCiManagement( Element root, CiManagement ciManagement )
184 if ( ciManagement == null )
189 Element elem = root.addElement( "ciManagement" );
190 addChildElement( elem, "system", ciManagement.getSystem() );
191 addChildElement( elem, "url", ciManagement.getUrl() );
192 // TODO: Add notifiers into ArchivaProjectModel
195 private void addDependencies( Element root, List<Dependency> dependencies )
197 if ( CollectionUtils.isEmpty( dependencies ) )
202 addDependencyList( root, dependencies );
205 private void addDependencyList( Element elemParent, List<Dependency> dependencies )
207 if ( CollectionUtils.isEmpty( dependencies ) )
212 Element elemDeps = elemParent.addElement( "dependencies" );
214 for ( Dependency dep : dependencies )
216 Element elem = elemDeps.addElement( "dependency" );
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() );
226 addExclusions( elem, dep.getExclusions() );
230 private void addDependencyManagement( Element root, List<Dependency> dependencyManagement )
232 if ( CollectionUtils.isEmpty( dependencyManagement ) )
237 Element elemDepMgmt = root.addElement( "dependencyManagement" );
238 addDependencyList( elemDepMgmt, dependencyManagement );
241 private void addDevelopersAndContributors( Element root, List<Individual> individuals )
243 if ( CollectionUtils.isEmpty( individuals ) )
248 Element developers = null;
249 Element contributors = null;
251 for ( Individual individual : individuals )
253 if ( individual.isCommitor() )
255 if ( developers == null )
257 developers = root.addElement( "developers" );
260 Element developer = developers.addElement( "developer" );
261 addChildElement( developer, "id", individual.getPrincipal() );
262 addIndividual( developer, individual );
266 if ( contributors == null )
268 contributors = root.addElement( "contributors" );
271 Element contributor = contributors.addElement( "contributor" );
272 addIndividual( contributor, individual );
277 private void addExclusions( Element elemParent, List<Exclusion> exclusions )
279 if ( CollectionUtils.isEmpty( exclusions ) )
284 Element elemExclusions = elemParent.addElement( "exclusions" );
286 for ( Exclusion exclusion : exclusions )
288 Element elem = elemExclusions.addElement( "exclusion" );
290 addChildElement( elem, "groupId", exclusion.getGroupId() );
291 addChildElement( elem, "artifactId", exclusion.getArtifactId() );
295 private void addIndividual( Element elem, Individual individual )
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() );
303 if ( CollectionUtils.isNotEmpty( individual.getRoles() ) )
305 Element roles = elem.addElement( "roles" );
306 List<String> roleList = individual.getRoles();
307 for ( String roleName : roleList )
309 addChildElement( roles, "role", roleName );
314 private void addIssueManagement( Element root, IssueManagement issueManagement )
316 if ( issueManagement == null )
321 Element elem = root.addElement( "issueManagement" );
322 addChildElement( elem, "system", issueManagement.getSystem() );
323 addChildElement( elem, "url", issueManagement.getUrl() );
326 private void addLicenses( Element root, List<License> licenses )
328 if ( CollectionUtils.isEmpty( licenses ) )
333 Element elemLicenses = root.addElement( "licenses" );
335 for ( License license : licenses )
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.
344 private void addMailingLists( Element root, List<MailingList> mailingLists )
346 if ( CollectionUtils.isEmpty( mailingLists ) )
351 Element mlists = root.addElement( "mailingLists" );
353 for ( MailingList mailingList : mailingLists )
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() );
362 addOtherArchives( mlist, mailingList.getOtherArchives() );
366 private void addOtherArchives( Element mlist, List<String> otherArchives )
368 if ( CollectionUtils.isEmpty( otherArchives ) )
373 Element elemOtherArchives = mlist.addElement( "otherArchives" );
375 for ( String archive : otherArchives )
377 addChildElement( elemOtherArchives, "otherArchive", archive );
381 private void addOrganization( Element root, Organization organization )
383 if ( organization == null )
388 Element elem = root.addElement( "organization" );
390 addChildElement( elem, "name", organization.getName() );
391 addChildElement( elem, "url", organization.getUrl() );
394 private void addParent( Element root, VersionedReference parentProject )
396 if ( parentProject == null )
401 Element parent = root.addElement( "parent" );
402 parent.addElement( "groupId" ).setText( parentProject.getGroupId() );
403 parent.addElement( "artifactId" ).setText( parentProject.getArtifactId() );
404 parent.addElement( "version" ).setText( parentProject.getVersion() );
407 private void addPlugins( Element root, List<ArtifactReference> plugins )
409 if ( CollectionUtils.isEmpty( plugins ) )
414 Element build = root.element( "build" );
417 build = root.addElement( "build" );
420 Element elemPlugins = build.addElement( "plugins" );
422 for ( ArtifactReference plugin : plugins )
424 Element elem = elemPlugins.addElement( "plugin" );
426 addArtifactReference( elem, plugin, "maven-plugin" );
430 private void addRelocation( Element root, VersionedReference relocation )
432 if ( relocation == null )
437 Element distribManagement = root.element( "distributionManagement" );
439 if ( distribManagement == null )
441 distribManagement = root.addElement( "distributionManagement" );
444 Element elem = distribManagement.addElement( "relocation" );
445 addChildElement( elem, "groupId", relocation.getGroupId() );
446 addChildElement( elem, "artifactId", relocation.getArtifactId() );
447 addChildElement( elem, "version", relocation.getVersion() );
450 private void addReporting( Element root, List<ArtifactReference> reports )
452 if ( CollectionUtils.isEmpty( reports ) )
457 Element reporting = root.addElement( "reporting" );
458 Element plugins = reporting.addElement( "plugins" );
460 for ( ArtifactReference reference : reports )
462 Element plugin = plugins.addElement( "plugin" );
463 addChildElement( plugin, "groupId", reference.getGroupId() );
464 addChildElement( plugin, "artifactId", reference.getArtifactId() );
465 addChildElement( plugin, "version", reference.getVersion() );
469 private void addRepositories( Element root, List<ProjectRepository> repositories )
471 if ( CollectionUtils.isEmpty( repositories ) )
476 Element elemRepos = root.addElement( "repositories" );
477 for ( ProjectRepository repository : repositories )
479 Element elem = elemRepos.addElement( "repository" );
480 addChildElement( elem, "id", repository.getId() );
481 addChildElement( elem, "name", repository.getName() );
482 addChildElement( elem, "url", repository.getUrl() );
484 if ( !StringUtils.equals( "default", repository.getLayout() ) )
486 addChildElement( elem, "layout", repository.getLayout() );
491 private void addScm( Element root, Scm scm )
498 Element elem = root.addElement( "scm" );
500 addChildElement( elem, "connection", scm.getConnection() );
501 addChildElement( elem, "developerConnection", scm.getDeveloperConnection() );
502 addChildElement( elem, "url", scm.getUrl() );
506 * Fix the default namespace on all elements recursively.
508 private void fixDefaultNamespace( Element elem )
510 elem.remove( elem.getNamespace() );
511 elem.setQName( QName.get( elem.getName(), DEFAULT_NAMESPACE, elem.getQualifiedName() ) );
515 Iterator<Node> it = elem.elementIterator();
516 while ( it.hasNext() )
520 switch ( n.getNodeType() )
522 case Node.ELEMENT_NODE:
523 fixDefaultNamespace( (Element) n );
529 private static void addChildElement( Element elem, String elemName, String text )
531 if ( StringUtils.isBlank( text ) )
536 elem.addElement( elemName ).setText( text );