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 public class ProjectModel400Reader
54 implements ProjectModelReader
56 public ArchivaProjectModel read( File pomFile )
59 XMLReader xml = new XMLReader( "project", pomFile );
61 ArchivaProjectModel model = new ArchivaProjectModel();
63 if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
65 // No namespace defined
66 // TODO: Output to monitor the problem with the Namespace.
69 xml.removeNamespaces();
71 Element project = xml.getElement( "//project" );
73 model.setGroupId( project.elementTextTrim( "groupId" ) );
74 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
75 model.setVersion( project.elementTextTrim( "version" ) );
76 model.setName( project.elementTextTrim( "name" ) );
77 model.setDescription( project.elementTextTrim( "description" ) );
78 model.setUrl( project.elementTextTrim( "url" ) );
80 model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
82 model.setParentProject( getParentProject( xml ) );
84 model.setMailingLists( getMailingLists( xml ) );
85 model.setCiManagement( getCiManagement( xml ) );
86 model.setIndividuals( getIndividuals( xml ) );
87 model.setIssueManagement( getIssueManagement( xml ) );
88 model.setLicenses( getLicenses( xml ) );
89 model.setOrganization( getOrganization( xml ) );
90 model.setScm( getSCM( xml ) );
91 model.setRepositories( getRepositories( xml ) );
93 model.setDependencies( getDependencies( xml ) );
94 model.setDependencyManagement( getDependencyManagement( xml ) );
95 model.setPlugins( getPlugins( xml ) );
96 model.setReports( getReports( xml ) );
97 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
99 model.setBuildExtensions( getBuildExtensions( xml ) );
101 model.setRelocation( getRelocation( xml ) );
103 model.setOrigin( "filesystem" );
108 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
110 ArtifactReference reference = new ArtifactReference();
112 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
113 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
114 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
115 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
116 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
122 * Get List of {@link ArtifactReference} objects from xpath expr.
124 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
127 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
129 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
130 while ( it.hasNext() )
132 Element elemPlugin = it.next();
134 refs.add( getArtifactReference( elemPlugin, defaultType ) );
140 private List<ArtifactReference> getBuildExtensions( XMLReader xml )
143 return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
146 private CiManagement getCiManagement( XMLReader xml )
149 Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
150 if ( elemCiMgmt != null )
152 CiManagement ciManagement = new CiManagement();
153 ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
154 ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
161 private List<Dependency> getDependencies( XMLReader xml )
164 return getDependencyList( xml, new String[] { "dependencies" } );
167 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
170 List<Dependency> dependencyList = new ArrayList<Dependency>();
172 Element project = xml.getElement( "//project" );
174 Element depsParent = project;
176 for ( String part : parts )
178 depsParent = depsParent.element( part );
179 if ( depsParent == null )
181 return dependencyList;
185 Iterator<Element> it = depsParent.elementIterator( "dependency" );
186 while ( it.hasNext() )
188 Element elemDependency = it.next();
189 Dependency dependency = new Dependency();
191 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
192 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
193 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
195 dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
196 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
197 dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
198 // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
199 dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
200 if ( DependencyScope.isSystemScoped( dependency ) )
202 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
205 dependency.setExclusions( getExclusions( elemDependency ) );
207 if ( dependencyList.contains( dependency ) )
209 // TODO: throw into monitor as "duplicate dependency" issue.
212 dependencyList.add( dependency );
215 return dependencyList;
218 private List<Dependency> getDependencyManagement( XMLReader xml )
221 return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
224 private List<Exclusion> getExclusions( Element elemDependency )
226 List<Exclusion> exclusions = new ArrayList<Exclusion>();
228 Element elemExclusions = elemDependency.element( "exclusions" );
230 if ( elemExclusions != null )
232 Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
233 while ( it.hasNext() )
235 Element elemExclusion = it.next();
236 Exclusion exclusion = new Exclusion();
238 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
239 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
241 exclusions.add( exclusion );
248 private List<Individual> getIndividuals( XMLReader xml )
251 List<Individual> individuals = new ArrayList<Individual>();
253 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
254 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
259 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
262 List<Individual> ret = new ArrayList<Individual>();
264 List<Element> modelPersonList = xml.getElementList( xpathExpr );
266 Iterator<Element> iter = modelPersonList.iterator();
267 while ( iter.hasNext() )
269 Element elemPerson = iter.next();
270 Individual individual = new Individual();
274 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
277 individual.setCommitor( isCommitor );
278 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
279 individual.setName( elemPerson.elementTextTrim( "name" ) );
280 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
281 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
282 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
283 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
286 Element elemRoles = elemPerson.element( "roles" );
287 if ( elemRoles != null )
289 List<Element> roleNames = elemRoles.elements( "role" );
290 Iterator<Element> itRole = roleNames.iterator();
291 while ( itRole.hasNext() )
293 Element role = itRole.next();
294 individual.addRole( role.getTextTrim() );
299 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
301 ret.add( individual );
307 private IssueManagement getIssueManagement( XMLReader xml )
310 Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
311 if ( elemIssueMgmt != null )
313 IssueManagement issueMgmt = new IssueManagement();
315 issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
316 issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
324 private List<License> getLicenses( XMLReader xml )
327 List<License> licenses = new ArrayList<License>();
329 Element elemLicenses = xml.getElement( "//project/licenses" );
331 if ( elemLicenses != null )
333 List<Element> licenseList = elemLicenses.elements( "license" );
334 for ( Element elemLicense : licenseList )
336 License license = new License();
338 // TODO: Create LicenseIdentity class to managed license ids.
339 // license.setId( elemLicense.elementTextTrim("id") );
340 license.setName( elemLicense.elementTextTrim( "name" ) );
341 license.setUrl( elemLicense.elementTextTrim( "url" ) );
342 license.setComments( elemLicense.elementTextTrim( "comments" ) );
344 licenses.add( license );
351 private List<MailingList> getMailingLists( XMLReader xml )
354 List<MailingList> mailingLists = new ArrayList<MailingList>();
356 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
357 for ( Element elemMailingList : mailingListElems )
359 MailingList mlist = new MailingList();
361 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
362 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
363 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
364 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
365 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
367 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
368 if ( elemOtherArchives != null )
370 List<String> otherArchives = new ArrayList<String>();
371 List<Element> others = elemOtherArchives.elements( "otherArchive" );
372 for ( Element other : others )
374 String otherArchive = other.getTextTrim();
375 otherArchives.add( otherArchive );
378 mlist.setOtherArchives( otherArchives );
381 mailingLists.add( mlist );
387 private Organization getOrganization( XMLReader xml )
390 Element elemOrg = xml.getElement( "//project/organization" );
391 if ( elemOrg != null )
393 Organization org = new Organization();
395 org.setName( elemOrg.elementTextTrim( "name" ) );
396 org.setUrl( elemOrg.elementTextTrim( "url" ) );
404 private VersionedReference getParentProject( XMLReader xml )
407 Element elemParent = xml.getElement( "//project/parent" );
409 if ( elemParent != null )
411 return getVersionedReference( elemParent );
417 private List<ArtifactReference> getPlugins( XMLReader xml )
420 return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
423 private Properties getProperties( Element elemProperties )
425 if ( elemProperties == null )
430 Properties ret = new Properties();
432 Iterator<Element> itProps = elemProperties.elements().iterator();
433 while ( itProps.hasNext() )
435 Element elemProp = (Element) itProps.next();
436 ret.setProperty( elemProp.getName(), elemProp.getText() );
442 private VersionedReference getRelocation( XMLReader xml )
445 Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
447 if ( elemRelocation != null )
449 return getVersionedReference( elemRelocation );
455 private List<ArtifactReference> getReports( XMLReader xml )
458 return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
461 private List<ProjectRepository> getRepositories( XMLReader xml )
464 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
466 repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
467 repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
472 private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
475 List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
477 List<Element> repositoriesList = xml.getElementList( xpathExpr );
479 for ( Element elemRepo : repositoriesList )
481 ProjectRepository repo = new ProjectRepository();
483 repo.setId( elemRepo.elementTextTrim( "id" ) );
484 repo.setName( elemRepo.elementTextTrim( "name" ) );
485 repo.setUrl( elemRepo.elementTextTrim( "url" ) );
486 repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
487 repo.setPlugins( isPluginRepo );
489 repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
490 repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
498 private Scm getSCM( XMLReader xml )
501 Element elemScm = xml.getElement( "//project/scm" );
503 if ( elemScm != null )
507 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
508 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
509 scm.setUrl( elemScm.elementTextTrim( "url" ) );
517 private VersionedReference getVersionedReference( Element elem )
519 VersionedReference reference = new VersionedReference();
521 reference.setGroupId( elem.elementTextTrim( "groupId" ) );
522 reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
523 reference.setVersion( elem.elementTextTrim( "version" ) );
528 private boolean toBoolean( String value, boolean defaultValue )
530 if ( StringUtils.equalsIgnoreCase( value, "true" ) )
534 else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
540 // If unset, or not "true" or "false".