1 package org.apache.maven.archiva.repository.project.readers;
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.lang.StringUtils;
23 import org.apache.maven.archiva.model.ArchivaProjectModel;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.model.CiManagement;
26 import org.apache.maven.archiva.model.Dependency;
27 import org.apache.maven.archiva.model.DependencyScope;
28 import org.apache.maven.archiva.model.Exclusion;
29 import org.apache.maven.archiva.model.Individual;
30 import org.apache.maven.archiva.model.IssueManagement;
31 import org.apache.maven.archiva.model.License;
32 import org.apache.maven.archiva.model.MailingList;
33 import org.apache.maven.archiva.model.Organization;
34 import org.apache.maven.archiva.model.ProjectRepository;
35 import org.apache.maven.archiva.model.Scm;
36 import org.apache.maven.archiva.model.VersionedReference;
37 import org.apache.maven.archiva.repository.project.ProjectModelException;
38 import org.apache.maven.archiva.repository.project.ProjectModelReader;
39 import org.apache.maven.archiva.xml.XMLException;
40 import org.apache.maven.archiva.xml.XMLReader;
41 import org.dom4j.Element;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Properties;
50 * ProjectModel400Reader - read in modelVersion 4.0.0 pom files into archiva-model structures.
52 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
56 * role="org.apache.maven.archiva.repository.project.ProjectModelReader"
57 * role-hint="model400"
59 public class ProjectModel400Reader
60 implements ProjectModelReader
63 public ArchivaProjectModel read( File pomFile )
64 throws ProjectModelException
68 XMLReader xml = new XMLReader( "project", pomFile );
70 ArchivaProjectModel model = new ArchivaProjectModel();
72 if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
74 // No namespace defined
75 // TODO: Output to monitor the problem with the Namespace.
78 xml.removeNamespaces();
80 Element project = xml.getElement( "//project" );
82 model.setGroupId( project.elementTextTrim( "groupId" ) );
83 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
84 model.setVersion( project.elementTextTrim( "version" ) );
85 model.setName( project.elementTextTrim( "name" ) );
86 model.setDescription( project.elementTextTrim( "description" ) );
87 model.setUrl( project.elementTextTrim( "url" ) );
89 model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
91 model.setParentProject( getParentProject( xml ) );
93 model.setMailingLists( getMailingLists( xml ) );
94 model.setCiManagement( getCiManagement( xml ) );
95 model.setIndividuals( getIndividuals( xml ) );
96 model.setIssueManagement( getIssueManagement( xml ) );
97 model.setLicenses( getLicenses( xml ) );
98 model.setOrganization( getOrganization( xml ) );
99 model.setScm( getSCM( xml ) );
100 model.setRepositories( getRepositories( xml ) );
102 model.setDependencies( getDependencies( xml ) );
103 model.setDependencyManagement( getDependencyManagement( xml ) );
104 model.setPlugins( getPlugins( xml ) );
105 model.setReports( getReports( xml ) );
106 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
108 model.setBuildExtensions( getBuildExtensions( xml ) );
110 model.setRelocation( getRelocation( xml ) );
114 catch ( XMLException e )
116 throw new ProjectModelException( e.getMessage(), e );
120 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
122 ArtifactReference reference = new ArtifactReference();
124 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
125 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
126 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
127 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
128 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
134 * Get List of {@link ArtifactReference} objects from xpath expr.
136 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
139 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
141 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
142 while ( it.hasNext() )
144 Element elemPlugin = it.next();
146 refs.add( getArtifactReference( elemPlugin, defaultType ) );
152 private List<ArtifactReference> getBuildExtensions( XMLReader xml )
155 return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
158 private CiManagement getCiManagement( XMLReader xml )
161 Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
162 if ( elemCiMgmt != null )
164 CiManagement ciManagement = new CiManagement();
165 ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
166 ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
173 private List<Dependency> getDependencies( XMLReader xml )
176 return getDependencyList( xml, new String[] { "dependencies" } );
179 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
182 List<Dependency> dependencyList = new ArrayList<Dependency>();
184 Element project = xml.getElement( "//project" );
186 Element depsParent = project;
188 for ( String part : parts )
190 depsParent = depsParent.element( part );
191 if ( depsParent == null )
193 return dependencyList;
197 Iterator<Element> it = depsParent.elementIterator( "dependency" );
198 while ( it.hasNext() )
200 Element elemDependency = it.next();
201 Dependency dependency = new Dependency();
203 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
204 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
205 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
207 dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
208 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
209 dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
210 // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
211 dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
212 if ( DependencyScope.isSystemScoped( dependency ) )
214 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
217 dependency.setExclusions( getExclusions( elemDependency ) );
219 if ( dependencyList.contains( dependency ) )
221 // TODO: throw into monitor as "duplicate dependency" issue.
224 dependencyList.add( dependency );
227 return dependencyList;
230 private List<Dependency> getDependencyManagement( XMLReader xml )
233 return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
236 private List<Exclusion> getExclusions( Element elemDependency )
238 List<Exclusion> exclusions = new ArrayList<Exclusion>();
240 Element elemExclusions = elemDependency.element( "exclusions" );
242 if ( elemExclusions != null )
244 Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
245 while ( it.hasNext() )
247 Element elemExclusion = it.next();
248 Exclusion exclusion = new Exclusion();
250 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
251 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
253 exclusions.add( exclusion );
260 private List<Individual> getIndividuals( XMLReader xml )
263 List<Individual> individuals = new ArrayList<Individual>();
265 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
266 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
271 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
274 List<Individual> ret = new ArrayList<Individual>();
276 List<Element> modelPersonList = xml.getElementList( xpathExpr );
278 Iterator<Element> iter = modelPersonList.iterator();
279 while ( iter.hasNext() )
281 Element elemPerson = iter.next();
282 Individual individual = new Individual();
286 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
289 individual.setCommitor( isCommitor );
290 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
291 individual.setName( elemPerson.elementTextTrim( "name" ) );
292 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
293 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
294 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
295 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
298 Element elemRoles = elemPerson.element( "roles" );
299 if ( elemRoles != null )
301 List<Element> roleNames = elemRoles.elements( "role" );
302 Iterator<Element> itRole = roleNames.iterator();
303 while ( itRole.hasNext() )
305 Element role = itRole.next();
306 individual.addRole( role.getTextTrim() );
311 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
313 ret.add( individual );
319 private IssueManagement getIssueManagement( XMLReader xml )
322 Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
323 if ( elemIssueMgmt != null )
325 IssueManagement issueMgmt = new IssueManagement();
327 issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
328 issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
336 private List<License> getLicenses( XMLReader xml )
339 List<License> licenses = new ArrayList<License>();
341 Element elemLicenses = xml.getElement( "//project/licenses" );
343 if ( elemLicenses != null )
345 List<Element> licenseList = elemLicenses.elements( "license" );
346 for ( Element elemLicense : licenseList )
348 License license = new License();
350 // TODO: Create LicenseIdentity class to managed license ids.
351 // license.setId( elemLicense.elementTextTrim("id") );
352 license.setName( elemLicense.elementTextTrim( "name" ) );
353 license.setUrl( elemLicense.elementTextTrim( "url" ) );
354 license.setComments( elemLicense.elementTextTrim( "comments" ) );
356 licenses.add( license );
363 private List<MailingList> getMailingLists( XMLReader xml )
366 List<MailingList> mailingLists = new ArrayList<MailingList>();
368 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
369 for ( Element elemMailingList : mailingListElems )
371 MailingList mlist = new MailingList();
373 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
374 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
375 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
376 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
377 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
379 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
380 if ( elemOtherArchives != null )
382 List<String> otherArchives = new ArrayList<String>();
383 List<Element> others = elemOtherArchives.elements( "otherArchive" );
384 for ( Element other : others )
386 String otherArchive = other.getTextTrim();
387 otherArchives.add( otherArchive );
390 mlist.setOtherArchives( otherArchives );
393 mailingLists.add( mlist );
399 private Organization getOrganization( XMLReader xml )
402 Element elemOrg = xml.getElement( "//project/organization" );
403 if ( elemOrg != null )
405 Organization org = new Organization();
407 org.setName( elemOrg.elementTextTrim( "name" ) );
408 org.setUrl( elemOrg.elementTextTrim( "url" ) );
416 private VersionedReference getParentProject( XMLReader xml )
419 Element elemParent = xml.getElement( "//project/parent" );
421 if ( elemParent != null )
423 return getVersionedReference( elemParent );
429 private List<ArtifactReference> getPlugins( XMLReader xml )
432 return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
435 private Properties getProperties( Element elemProperties )
437 if ( elemProperties == null )
442 Properties ret = new Properties();
444 Iterator<Element> itProps = elemProperties.elements().iterator();
445 while ( itProps.hasNext() )
447 Element elemProp = (Element) itProps.next();
448 ret.setProperty( elemProp.getName(), elemProp.getText() );
454 private VersionedReference getRelocation( XMLReader xml )
457 Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
459 if ( elemRelocation != null )
461 return getVersionedReference( elemRelocation );
467 private List<ArtifactReference> getReports( XMLReader xml )
470 return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
473 private List<ProjectRepository> getRepositories( XMLReader xml )
476 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
478 repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
479 repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
484 private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
487 List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
489 List<Element> repositoriesList = xml.getElementList( xpathExpr );
491 for ( Element elemRepo : repositoriesList )
493 ProjectRepository repo = new ProjectRepository();
495 repo.setId( elemRepo.elementTextTrim( "id" ) );
496 repo.setName( elemRepo.elementTextTrim( "name" ) );
497 repo.setUrl( elemRepo.elementTextTrim( "url" ) );
498 repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
499 repo.setPlugins( isPluginRepo );
501 repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
502 repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
510 private Scm getSCM( XMLReader xml )
513 Element elemScm = xml.getElement( "//project/scm" );
515 if ( elemScm != null )
519 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
520 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
521 scm.setUrl( elemScm.elementTextTrim( "url" ) );
529 private VersionedReference getVersionedReference( Element elem )
531 VersionedReference reference = new VersionedReference();
533 reference.setGroupId( elem.elementTextTrim( "groupId" ) );
534 reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
535 reference.setVersion( elem.elementTextTrim( "version" ) );
540 private boolean toBoolean( String value, boolean defaultValue )
542 if ( StringUtils.equalsIgnoreCase( value, "true" ) )
546 else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
552 // If unset, or not "true" or "false".