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.
54 public class ProjectModel400Reader
55 implements ProjectModelReader
58 public ArchivaProjectModel read( File pomFile )
59 throws ProjectModelException
63 XMLReader xml = new XMLReader( "project", pomFile );
65 ArchivaProjectModel model = new ArchivaProjectModel();
67 if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
69 // No namespace defined
70 // TODO: Output to monitor the problem with the Namespace.
73 xml.removeNamespaces();
75 Element project = xml.getElement( "//project" );
77 model.setGroupId( project.elementTextTrim( "groupId" ) );
78 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
79 model.setVersion( project.elementTextTrim( "version" ) );
80 model.setName( project.elementTextTrim( "name" ) );
81 model.setDescription( project.elementTextTrim( "description" ) );
82 model.setUrl( project.elementTextTrim( "url" ) );
84 model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
86 model.setParentProject( getParentProject( xml ) );
88 model.setMailingLists( getMailingLists( xml ) );
89 model.setCiManagement( getCiManagement( xml ) );
90 model.setIndividuals( getIndividuals( xml ) );
91 model.setIssueManagement( getIssueManagement( xml ) );
92 model.setLicenses( getLicenses( xml ) );
93 model.setOrganization( getOrganization( xml ) );
94 model.setScm( getSCM( xml ) );
95 model.setRepositories( getRepositories( xml ) );
97 model.setDependencies( getDependencies( xml ) );
98 model.setDependencyManagement( getDependencyManagement( xml ) );
99 model.setPlugins( getPlugins( xml ) );
100 model.setReports( getReports( xml ) );
101 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
103 model.setBuildExtensions( getBuildExtensions( xml ) );
105 model.setRelocation( getRelocation( xml ) );
107 model.setOrigin("filesystem");
111 catch ( XMLException e )
113 throw new ProjectModelException( e.getMessage(), e );
117 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
119 ArtifactReference reference = new ArtifactReference();
121 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
122 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
123 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
124 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
125 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
131 * Get List of {@link ArtifactReference} objects from xpath expr.
133 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
136 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
138 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
139 while ( it.hasNext() )
141 Element elemPlugin = it.next();
143 refs.add( getArtifactReference( elemPlugin, defaultType ) );
149 private List<ArtifactReference> getBuildExtensions( XMLReader xml )
152 return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
155 private CiManagement getCiManagement( XMLReader xml )
158 Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
159 if ( elemCiMgmt != null )
161 CiManagement ciManagement = new CiManagement();
162 ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
163 ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
170 private List<Dependency> getDependencies( XMLReader xml )
173 return getDependencyList( xml, new String[] { "dependencies" } );
176 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
179 List<Dependency> dependencyList = new ArrayList<Dependency>();
181 Element project = xml.getElement( "//project" );
183 Element depsParent = project;
185 for ( String part : parts )
187 depsParent = depsParent.element( part );
188 if ( depsParent == null )
190 return dependencyList;
194 Iterator<Element> it = depsParent.elementIterator( "dependency" );
195 while ( it.hasNext() )
197 Element elemDependency = it.next();
198 Dependency dependency = new Dependency();
200 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
201 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
202 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
204 dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
205 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
206 dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
207 // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
208 dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
209 if ( DependencyScope.isSystemScoped( dependency ) )
211 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
214 dependency.setExclusions( getExclusions( elemDependency ) );
216 if ( dependencyList.contains( dependency ) )
218 // TODO: throw into monitor as "duplicate dependency" issue.
221 dependencyList.add( dependency );
224 return dependencyList;
227 private List<Dependency> getDependencyManagement( XMLReader xml )
230 return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
233 private List<Exclusion> getExclusions( Element elemDependency )
235 List<Exclusion> exclusions = new ArrayList<Exclusion>();
237 Element elemExclusions = elemDependency.element( "exclusions" );
239 if ( elemExclusions != null )
241 Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
242 while ( it.hasNext() )
244 Element elemExclusion = it.next();
245 Exclusion exclusion = new Exclusion();
247 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
248 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
250 exclusions.add( exclusion );
257 private List<Individual> getIndividuals( XMLReader xml )
260 List<Individual> individuals = new ArrayList<Individual>();
262 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
263 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
268 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
271 List<Individual> ret = new ArrayList<Individual>();
273 List<Element> modelPersonList = xml.getElementList( xpathExpr );
275 Iterator<Element> iter = modelPersonList.iterator();
276 while ( iter.hasNext() )
278 Element elemPerson = iter.next();
279 Individual individual = new Individual();
283 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
286 individual.setCommitor( isCommitor );
287 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
288 individual.setName( elemPerson.elementTextTrim( "name" ) );
289 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
290 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
291 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
292 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
295 Element elemRoles = elemPerson.element( "roles" );
296 if ( elemRoles != null )
298 List<Element> roleNames = elemRoles.elements( "role" );
299 Iterator<Element> itRole = roleNames.iterator();
300 while ( itRole.hasNext() )
302 Element role = itRole.next();
303 individual.addRole( role.getTextTrim() );
308 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
310 ret.add( individual );
316 private IssueManagement getIssueManagement( XMLReader xml )
319 Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
320 if ( elemIssueMgmt != null )
322 IssueManagement issueMgmt = new IssueManagement();
324 issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
325 issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
333 private List<License> getLicenses( XMLReader xml )
336 List<License> licenses = new ArrayList<License>();
338 Element elemLicenses = xml.getElement( "//project/licenses" );
340 if ( elemLicenses != null )
342 List<Element> licenseList = elemLicenses.elements( "license" );
343 for ( Element elemLicense : licenseList )
345 License license = new License();
347 // TODO: Create LicenseIdentity class to managed license ids.
348 // license.setId( elemLicense.elementTextTrim("id") );
349 license.setName( elemLicense.elementTextTrim( "name" ) );
350 license.setUrl( elemLicense.elementTextTrim( "url" ) );
351 license.setComments( elemLicense.elementTextTrim( "comments" ) );
353 licenses.add( license );
360 private List<MailingList> getMailingLists( XMLReader xml )
363 List<MailingList> mailingLists = new ArrayList<MailingList>();
365 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
366 for ( Element elemMailingList : mailingListElems )
368 MailingList mlist = new MailingList();
370 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
371 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
372 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
373 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
374 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
376 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
377 if ( elemOtherArchives != null )
379 List<String> otherArchives = new ArrayList<String>();
380 List<Element> others = elemOtherArchives.elements( "otherArchive" );
381 for ( Element other : others )
383 String otherArchive = other.getTextTrim();
384 otherArchives.add( otherArchive );
387 mlist.setOtherArchives( otherArchives );
390 mailingLists.add( mlist );
396 private Organization getOrganization( XMLReader xml )
399 Element elemOrg = xml.getElement( "//project/organization" );
400 if ( elemOrg != null )
402 Organization org = new Organization();
404 org.setName( elemOrg.elementTextTrim( "name" ) );
405 org.setUrl( elemOrg.elementTextTrim( "url" ) );
413 private VersionedReference getParentProject( XMLReader xml )
416 Element elemParent = xml.getElement( "//project/parent" );
418 if ( elemParent != null )
420 return getVersionedReference( elemParent );
426 private List<ArtifactReference> getPlugins( XMLReader xml )
429 return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
432 private Properties getProperties( Element elemProperties )
434 if ( elemProperties == null )
439 Properties ret = new Properties();
441 Iterator<Element> itProps = elemProperties.elements().iterator();
442 while ( itProps.hasNext() )
444 Element elemProp = (Element) itProps.next();
445 ret.setProperty( elemProp.getName(), elemProp.getText() );
451 private VersionedReference getRelocation( XMLReader xml )
454 Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
456 if ( elemRelocation != null )
458 return getVersionedReference( elemRelocation );
464 private List<ArtifactReference> getReports( XMLReader xml )
467 return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
470 private List<ProjectRepository> getRepositories( XMLReader xml )
473 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
475 repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
476 repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
481 private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
484 List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
486 List<Element> repositoriesList = xml.getElementList( xpathExpr );
488 for ( Element elemRepo : repositoriesList )
490 ProjectRepository repo = new ProjectRepository();
492 repo.setId( elemRepo.elementTextTrim( "id" ) );
493 repo.setName( elemRepo.elementTextTrim( "name" ) );
494 repo.setUrl( elemRepo.elementTextTrim( "url" ) );
495 repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
496 repo.setPlugins( isPluginRepo );
498 repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
499 repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
507 private Scm getSCM( XMLReader xml )
510 Element elemScm = xml.getElement( "//project/scm" );
512 if ( elemScm != null )
516 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
517 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
518 scm.setUrl( elemScm.elementTextTrim( "url" ) );
526 private VersionedReference getVersionedReference( Element elem )
528 VersionedReference reference = new VersionedReference();
530 reference.setGroupId( elem.elementTextTrim( "groupId" ) );
531 reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
532 reference.setVersion( elem.elementTextTrim( "version" ) );
537 private boolean toBoolean( String value, boolean defaultValue )
539 if ( StringUtils.equalsIgnoreCase( value, "true" ) )
543 else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
549 // If unset, or not "true" or "false".