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
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Properties;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.model.ArchivaProjectModel;
30 import org.apache.maven.archiva.model.ArtifactReference;
31 import org.apache.maven.archiva.model.CiManagement;
32 import org.apache.maven.archiva.model.Dependency;
33 import org.apache.maven.archiva.model.DependencyScope;
34 import org.apache.maven.archiva.model.Exclusion;
35 import org.apache.maven.archiva.model.Individual;
36 import org.apache.maven.archiva.model.IssueManagement;
37 import org.apache.maven.archiva.model.License;
38 import org.apache.maven.archiva.model.MailingList;
39 import org.apache.maven.archiva.model.Organization;
40 import org.apache.maven.archiva.model.ProjectRepository;
41 import org.apache.maven.archiva.model.Scm;
42 import org.apache.maven.archiva.model.VersionedReference;
43 import org.apache.maven.archiva.repository.project.ProjectModelReader;
44 import org.apache.maven.archiva.xml.XMLException;
45 import org.apache.maven.archiva.xml.XMLReader;
46 import org.dom4j.Element;
49 * ProjectModel400Reader - read in modelVersion 4.0.0 pom files into archiva-model structures.
53 @SuppressWarnings("unchecked")
54 public class ProjectModel400Reader
55 implements ProjectModelReader
57 public ArchivaProjectModel read( File pomFile )
60 XMLReader xml = new XMLReader( "project", pomFile );
62 ArchivaProjectModel model = new ArchivaProjectModel();
64 if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
66 // No namespace defined
67 // TODO: Output to monitor the problem with the Namespace.
70 xml.removeNamespaces();
72 Element project = xml.getElement( "//project" );
74 model.setGroupId( project.elementTextTrim( "groupId" ) );
75 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
76 model.setVersion( project.elementTextTrim( "version" ) );
77 model.setName( project.elementTextTrim( "name" ) );
78 model.setDescription( project.elementTextTrim( "description" ) );
79 model.setUrl( project.elementTextTrim( "url" ) );
81 model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
83 model.setParentProject( getParentProject( xml ) );
85 model.setMailingLists( getMailingLists( xml ) );
86 model.setCiManagement( getCiManagement( xml ) );
87 model.setIndividuals( getIndividuals( xml ) );
88 model.setIssueManagement( getIssueManagement( xml ) );
89 model.setLicenses( getLicenses( xml ) );
90 model.setOrganization( getOrganization( xml ) );
91 model.setScm( getSCM( xml ) );
92 model.setRepositories( getRepositories( xml ) );
94 model.setDependencies( getDependencies( xml ) );
95 model.setDependencyManagement( getDependencyManagement( xml ) );
96 model.setPlugins( getPlugins( xml ) );
97 model.setReports( getReports( xml ) );
98 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
100 model.setBuildExtensions( getBuildExtensions( xml ) );
102 model.setRelocation( getRelocation( xml ) );
104 model.setOrigin( "filesystem" );
109 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
111 ArtifactReference reference = new ArtifactReference();
113 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
114 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
115 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
116 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
117 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
123 * Get List of {@link ArtifactReference} objects from xpath expr.
125 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
128 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
130 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
131 while ( it.hasNext() )
133 Element elemPlugin = it.next();
135 refs.add( getArtifactReference( elemPlugin, defaultType ) );
141 private List<ArtifactReference> getBuildExtensions( XMLReader xml )
144 return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
147 private CiManagement getCiManagement( XMLReader xml )
150 Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
151 if ( elemCiMgmt != null )
153 CiManagement ciManagement = new CiManagement();
154 ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
155 ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
162 private List<Dependency> getDependencies( XMLReader xml )
165 return getDependencyList( xml, new String[] { "dependencies" } );
168 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
171 List<Dependency> dependencyList = new ArrayList<Dependency>();
173 Element project = xml.getElement( "//project" );
175 Element depsParent = project;
177 for ( String part : parts )
179 depsParent = depsParent.element( part );
180 if ( depsParent == null )
182 return dependencyList;
186 Iterator<Element> it = depsParent.elementIterator( "dependency" );
187 while ( it.hasNext() )
189 Element elemDependency = it.next();
190 Dependency dependency = new Dependency();
192 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
193 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
194 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
196 dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
197 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
198 dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
199 // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
200 dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
201 if ( DependencyScope.isSystemScoped( dependency ) )
203 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
206 dependency.setExclusions( getExclusions( elemDependency ) );
208 if ( dependencyList.contains( dependency ) )
210 // TODO: throw into monitor as "duplicate dependency" issue.
213 dependencyList.add( dependency );
216 return dependencyList;
219 private List<Dependency> getDependencyManagement( XMLReader xml )
222 return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
225 private List<Exclusion> getExclusions( Element elemDependency )
227 List<Exclusion> exclusions = new ArrayList<Exclusion>();
229 Element elemExclusions = elemDependency.element( "exclusions" );
231 if ( elemExclusions != null )
233 Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
234 while ( it.hasNext() )
236 Element elemExclusion = it.next();
237 Exclusion exclusion = new Exclusion();
239 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
240 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
242 exclusions.add( exclusion );
249 private List<Individual> getIndividuals( XMLReader xml )
252 List<Individual> individuals = new ArrayList<Individual>();
254 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
255 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
260 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
263 List<Individual> ret = new ArrayList<Individual>();
265 List<Element> modelPersonList = xml.getElementList( xpathExpr );
267 Iterator<Element> iter = modelPersonList.iterator();
268 while ( iter.hasNext() )
270 Element elemPerson = iter.next();
271 Individual individual = new Individual();
275 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
278 individual.setCommitor( isCommitor );
279 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
280 individual.setName( elemPerson.elementTextTrim( "name" ) );
281 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
282 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
283 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
284 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
287 Element elemRoles = elemPerson.element( "roles" );
288 if ( elemRoles != null )
290 List<Element> roleNames = elemRoles.elements( "role" );
291 Iterator<Element> itRole = roleNames.iterator();
292 while ( itRole.hasNext() )
294 Element role = itRole.next();
295 individual.addRole( role.getTextTrim() );
300 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
302 ret.add( individual );
308 private IssueManagement getIssueManagement( XMLReader xml )
311 Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
312 if ( elemIssueMgmt != null )
314 IssueManagement issueMgmt = new IssueManagement();
316 issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
317 issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
325 private List<License> getLicenses( XMLReader xml )
328 List<License> licenses = new ArrayList<License>();
330 Element elemLicenses = xml.getElement( "//project/licenses" );
332 if ( elemLicenses != null )
334 List<Element> licenseList = elemLicenses.elements( "license" );
335 for ( Element elemLicense : licenseList )
337 License license = new License();
339 // TODO: Create LicenseIdentity class to managed license ids.
340 // license.setId( elemLicense.elementTextTrim("id") );
341 license.setName( elemLicense.elementTextTrim( "name" ) );
342 license.setUrl( elemLicense.elementTextTrim( "url" ) );
343 license.setComments( elemLicense.elementTextTrim( "comments" ) );
345 licenses.add( license );
352 private List<MailingList> getMailingLists( XMLReader xml )
355 List<MailingList> mailingLists = new ArrayList<MailingList>();
357 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
358 for ( Element elemMailingList : mailingListElems )
360 MailingList mlist = new MailingList();
362 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
363 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
364 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
365 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
366 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
368 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
369 if ( elemOtherArchives != null )
371 List<String> otherArchives = new ArrayList<String>();
372 List<Element> others = elemOtherArchives.elements( "otherArchive" );
373 for ( Element other : others )
375 String otherArchive = other.getTextTrim();
376 otherArchives.add( otherArchive );
379 mlist.setOtherArchives( otherArchives );
382 mailingLists.add( mlist );
388 private Organization getOrganization( XMLReader xml )
391 Element elemOrg = xml.getElement( "//project/organization" );
392 if ( elemOrg != null )
394 Organization org = new Organization();
396 org.setName( elemOrg.elementTextTrim( "name" ) );
397 org.setUrl( elemOrg.elementTextTrim( "url" ) );
405 private VersionedReference getParentProject( XMLReader xml )
408 Element elemParent = xml.getElement( "//project/parent" );
410 if ( elemParent != null )
412 return getVersionedReference( elemParent );
418 private List<ArtifactReference> getPlugins( XMLReader xml )
421 return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
424 private Properties getProperties( Element elemProperties )
426 if ( elemProperties == null )
431 Properties ret = new Properties();
433 Iterator<Element> itProps = elemProperties.elements().iterator();
434 while ( itProps.hasNext() )
436 Element elemProp = (Element) itProps.next();
437 ret.setProperty( elemProp.getName(), elemProp.getText() );
443 private VersionedReference getRelocation( XMLReader xml )
446 Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
448 if ( elemRelocation != null )
450 return getVersionedReference( elemRelocation );
456 private List<ArtifactReference> getReports( XMLReader xml )
459 return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
462 private List<ProjectRepository> getRepositories( XMLReader xml )
465 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
467 repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
468 repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
473 private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
476 List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
478 List<Element> repositoriesList = xml.getElementList( xpathExpr );
480 for ( Element elemRepo : repositoriesList )
482 ProjectRepository repo = new ProjectRepository();
484 repo.setId( elemRepo.elementTextTrim( "id" ) );
485 repo.setName( elemRepo.elementTextTrim( "name" ) );
486 repo.setUrl( elemRepo.elementTextTrim( "url" ) );
487 repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
488 repo.setPlugins( isPluginRepo );
490 repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
491 repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
499 private Scm getSCM( XMLReader xml )
502 Element elemScm = xml.getElement( "//project/scm" );
504 if ( elemScm != null )
508 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
509 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
510 scm.setUrl( elemScm.elementTextTrim( "url" ) );
518 private VersionedReference getVersionedReference( Element elem )
520 VersionedReference reference = new VersionedReference();
522 reference.setGroupId( elem.elementTextTrim( "groupId" ) );
523 reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
524 reference.setVersion( elem.elementTextTrim( "version" ) );
529 private boolean toBoolean( String value, boolean defaultValue )
531 if ( StringUtils.equalsIgnoreCase( value, "true" ) )
535 else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
541 // If unset, or not "true" or "false".