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>
55 public class ProjectModel400Reader
56 implements ProjectModelReader
59 public ArchivaProjectModel read( File pomFile )
60 throws ProjectModelException
64 XMLReader xml = new XMLReader( "project", pomFile );
66 ArchivaProjectModel model = new ArchivaProjectModel();
68 if ( !"http://maven.apache.org/POM/4.0.0".equals( xml.getDefaultNamespaceURI() ) )
70 // No namespace defined
71 // TODO: Output to monitor the problem with the Namespace.
74 xml.removeNamespaces();
76 Element project = xml.getElement( "//project" );
78 model.setGroupId( project.elementTextTrim( "groupId" ) );
79 model.setArtifactId( project.elementTextTrim( "artifactId" ) );
80 model.setVersion( project.elementTextTrim( "version" ) );
81 model.setName( project.elementTextTrim( "name" ) );
82 model.setDescription( project.elementTextTrim( "description" ) );
83 model.setUrl( project.elementTextTrim( "url" ) );
85 model.setPackaging( StringUtils.defaultIfEmpty( project.elementTextTrim( "packaging" ), "jar" ) );
87 model.setParentProject( getParentProject( xml ) );
89 model.setMailingLists( getMailingLists( xml ) );
90 model.setCiManagement( getCiManagement( xml ) );
91 model.setIndividuals( getIndividuals( xml ) );
92 model.setIssueManagement( getIssueManagement( xml ) );
93 model.setLicenses( getLicenses( xml ) );
94 model.setOrganization( getOrganization( xml ) );
95 model.setScm( getSCM( xml ) );
96 model.setRepositories( getRepositories( xml ) );
98 model.setDependencies( getDependencies( xml ) );
99 model.setDependencyManagement( getDependencyManagement( xml ) );
100 model.setPlugins( getPlugins( xml ) );
101 model.setReports( getReports( xml ) );
102 model.setProperties( getProperties( xml.getElement( "//project/properties" ) ) );
104 model.setBuildExtensions( getBuildExtensions( xml ) );
106 model.setRelocation( getRelocation( xml ) );
110 catch ( XMLException e )
112 throw new ProjectModelException( e.getMessage(), e );
116 private ArtifactReference getArtifactReference( Element elemPlugin, String defaultType )
118 ArtifactReference reference = new ArtifactReference();
120 reference.setGroupId( StringUtils.defaultString( elemPlugin.elementTextTrim( "groupId" ) ) );
121 reference.setArtifactId( elemPlugin.elementTextTrim( "artifactId" ) );
122 reference.setVersion( StringUtils.defaultString( elemPlugin.elementTextTrim( "version" ) ) );
123 reference.setClassifier( StringUtils.defaultString( elemPlugin.elementTextTrim( "classifier" ) ) );
124 reference.setType( StringUtils.defaultIfEmpty( elemPlugin.elementTextTrim( "type" ), defaultType ) );
130 * Get List of {@link ArtifactReference} objects from xpath expr.
132 private List<ArtifactReference> getArtifactReferenceList( XMLReader xml, String xpathExpr, String defaultType )
135 List<ArtifactReference> refs = new ArrayList<ArtifactReference>();
137 Iterator<Element> it = xml.getElementList( xpathExpr ).iterator();
138 while ( it.hasNext() )
140 Element elemPlugin = it.next();
142 refs.add( getArtifactReference( elemPlugin, defaultType ) );
148 private List<ArtifactReference> getBuildExtensions( XMLReader xml )
151 return getArtifactReferenceList( xml, "//project/build/extensions/extension", "jar" );
154 private CiManagement getCiManagement( XMLReader xml )
157 Element elemCiMgmt = xml.getElement( "//project/ciManagement" );
158 if ( elemCiMgmt != null )
160 CiManagement ciManagement = new CiManagement();
161 ciManagement.setSystem( elemCiMgmt.elementTextTrim( "system" ) );
162 ciManagement.setUrl( elemCiMgmt.elementTextTrim( "url" ) );
169 private List<Dependency> getDependencies( XMLReader xml )
172 return getDependencyList( xml, new String[] { "dependencies" } );
175 private List<Dependency> getDependencyList( XMLReader xml, String parts[] )
178 List<Dependency> dependencyList = new ArrayList<Dependency>();
180 Element project = xml.getElement( "//project" );
182 Element depsParent = project;
184 for ( String part : parts )
186 depsParent = depsParent.element( part );
187 if ( depsParent == null )
189 return dependencyList;
193 Iterator<Element> it = depsParent.elementIterator( "dependency" );
194 while ( it.hasNext() )
196 Element elemDependency = it.next();
197 Dependency dependency = new Dependency();
199 dependency.setGroupId( elemDependency.elementTextTrim( "groupId" ) );
200 dependency.setArtifactId( elemDependency.elementTextTrim( "artifactId" ) );
201 dependency.setVersion( elemDependency.elementTextTrim( "version" ) );
203 dependency.setClassifier( StringUtils.defaultString( elemDependency.elementTextTrim( "classifier" ) ) );
204 dependency.setType( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "type" ), "jar" ) );
205 dependency.setScope( StringUtils.defaultIfEmpty( elemDependency.elementTextTrim( "scope" ), "compile" ) );
206 // Not for v4.0.0 -> dependency.setUrl( elemDependency.elementTextTrim("url") );
207 dependency.setOptional( toBoolean( elemDependency.elementTextTrim( "optional" ), false ) );
208 if ( DependencyScope.isSystemScoped( dependency ) )
210 dependency.setSystemPath( elemDependency.elementTextTrim( "systemPath" ) );
213 dependency.setExclusions( getExclusions( elemDependency ) );
215 if ( dependencyList.contains( dependency ) )
217 // TODO: throw into monitor as "duplicate dependency" issue.
220 dependencyList.add( dependency );
223 return dependencyList;
226 private List<Dependency> getDependencyManagement( XMLReader xml )
229 return getDependencyList( xml, new String[] { "dependencyManagement", "dependencies" } );
232 private List<Exclusion> getExclusions( Element elemDependency )
234 List<Exclusion> exclusions = new ArrayList<Exclusion>();
236 Element elemExclusions = elemDependency.element( "exclusions" );
238 if ( elemExclusions != null )
240 Iterator<Element> it = elemExclusions.elementIterator( "exclusion" );
241 while ( it.hasNext() )
243 Element elemExclusion = it.next();
244 Exclusion exclusion = new Exclusion();
246 exclusion.setGroupId( elemExclusion.elementTextTrim( "groupId" ) );
247 exclusion.setArtifactId( elemExclusion.elementTextTrim( "artifactId" ) );
249 exclusions.add( exclusion );
256 private List<Individual> getIndividuals( XMLReader xml )
259 List<Individual> individuals = new ArrayList<Individual>();
261 individuals.addAll( getIndividuals( xml, true, "//project/developers/developer" ) );
262 individuals.addAll( getIndividuals( xml, false, "//project/contributors/contributor" ) );
267 private List<Individual> getIndividuals( XMLReader xml, boolean isCommitor, String xpathExpr )
270 List<Individual> ret = new ArrayList<Individual>();
272 List<Element> modelPersonList = xml.getElementList( xpathExpr );
274 Iterator<Element> iter = modelPersonList.iterator();
275 while ( iter.hasNext() )
277 Element elemPerson = iter.next();
278 Individual individual = new Individual();
282 individual.setPrincipal( elemPerson.elementTextTrim( "id" ) );
285 individual.setCommitor( isCommitor );
286 individual.setEmail( elemPerson.elementTextTrim( "email" ) );
287 individual.setName( elemPerson.elementTextTrim( "name" ) );
288 individual.setOrganization( elemPerson.elementTextTrim( "organization" ) );
289 individual.setOrganizationUrl( elemPerson.elementTextTrim( "organizationUrl" ) );
290 individual.setUrl( elemPerson.elementTextTrim( "url" ) );
291 individual.setTimezone( elemPerson.elementTextTrim( "timezone" ) );
294 Element elemRoles = elemPerson.element( "roles" );
295 if ( elemRoles != null )
297 List<Element> roleNames = elemRoles.elements( "role" );
298 Iterator<Element> itRole = roleNames.iterator();
299 while ( itRole.hasNext() )
301 Element role = itRole.next();
302 individual.addRole( role.getTextTrim() );
307 individual.setProperties( getProperties( elemPerson.element( "properties" ) ) );
309 ret.add( individual );
315 private IssueManagement getIssueManagement( XMLReader xml )
318 Element elemIssueMgmt = xml.getElement( "//project/issueManagement" );
319 if ( elemIssueMgmt != null )
321 IssueManagement issueMgmt = new IssueManagement();
323 issueMgmt.setSystem( elemIssueMgmt.elementTextTrim( "system" ) );
324 issueMgmt.setUrl( elemIssueMgmt.elementTextTrim( "url" ) );
332 private List<License> getLicenses( XMLReader xml )
335 List<License> licenses = new ArrayList<License>();
337 Element elemLicenses = xml.getElement( "//project/licenses" );
339 if ( elemLicenses != null )
341 List<Element> licenseList = elemLicenses.elements( "license" );
342 for ( Element elemLicense : licenseList )
344 License license = new License();
346 // TODO: Create LicenseIdentity class to managed license ids.
347 // license.setId( elemLicense.elementTextTrim("id") );
348 license.setName( elemLicense.elementTextTrim( "name" ) );
349 license.setUrl( elemLicense.elementTextTrim( "url" ) );
350 license.setComments( elemLicense.elementTextTrim( "comments" ) );
352 licenses.add( license );
359 private List<MailingList> getMailingLists( XMLReader xml )
362 List<MailingList> mailingLists = new ArrayList<MailingList>();
364 List<Element> mailingListElems = xml.getElementList( "//project/mailingLists/mailingList" );
365 for ( Element elemMailingList : mailingListElems )
367 MailingList mlist = new MailingList();
369 mlist.setName( elemMailingList.elementTextTrim( "name" ) );
370 mlist.setSubscribeAddress( elemMailingList.elementTextTrim( "subscribe" ) );
371 mlist.setUnsubscribeAddress( elemMailingList.elementTextTrim( "unsubscribe" ) );
372 mlist.setPostAddress( elemMailingList.elementTextTrim( "post" ) );
373 mlist.setMainArchiveUrl( elemMailingList.elementTextTrim( "archive" ) );
375 Element elemOtherArchives = elemMailingList.element( "otherArchives" );
376 if ( elemOtherArchives != null )
378 List<String> otherArchives = new ArrayList<String>();
379 List<Element> others = elemOtherArchives.elements( "otherArchive" );
380 for ( Element other : others )
382 String otherArchive = other.getTextTrim();
383 otherArchives.add( otherArchive );
386 mlist.setOtherArchives( otherArchives );
389 mailingLists.add( mlist );
395 private Organization getOrganization( XMLReader xml )
398 Element elemOrg = xml.getElement( "//project/organization" );
399 if ( elemOrg != null )
401 Organization org = new Organization();
403 org.setName( elemOrg.elementTextTrim( "name" ) );
404 org.setUrl( elemOrg.elementTextTrim( "url" ) );
412 private VersionedReference getParentProject( XMLReader xml )
415 Element elemParent = xml.getElement( "//project/parent" );
417 if ( elemParent != null )
419 return getVersionedReference( elemParent );
425 private List<ArtifactReference> getPlugins( XMLReader xml )
428 return getArtifactReferenceList( xml, "//project/build/plugins/plugin", "maven-plugin" );
431 private Properties getProperties( Element elemProperties )
433 if ( elemProperties == null )
438 Properties ret = new Properties();
440 Iterator<Element> itProps = elemProperties.elements().iterator();
441 while ( itProps.hasNext() )
443 Element elemProp = (Element) itProps.next();
444 ret.setProperty( elemProp.getName(), elemProp.getText() );
450 private VersionedReference getRelocation( XMLReader xml )
453 Element elemRelocation = xml.getElement( "//project/distributionManagement/relocation" );
455 if ( elemRelocation != null )
457 return getVersionedReference( elemRelocation );
463 private List<ArtifactReference> getReports( XMLReader xml )
466 return getArtifactReferenceList( xml, "//project/reporting/plugins/plugin", "maven-plugin" );
469 private List<ProjectRepository> getRepositories( XMLReader xml )
472 List<ProjectRepository> repos = new ArrayList<ProjectRepository>();
474 repos.addAll( getRepositories( xml, false, "//project/repositories/repository" ) );
475 repos.addAll( getRepositories( xml, true, "//project/pluginRepositories/pluginRepository" ) );
480 private List<ProjectRepository> getRepositories( XMLReader xml, boolean isPluginRepo, String xpathExpr )
483 List<ProjectRepository> ret = new ArrayList<ProjectRepository>();
485 List<Element> repositoriesList = xml.getElementList( xpathExpr );
487 for ( Element elemRepo : repositoriesList )
489 ProjectRepository repo = new ProjectRepository();
491 repo.setId( elemRepo.elementTextTrim( "id" ) );
492 repo.setName( elemRepo.elementTextTrim( "name" ) );
493 repo.setUrl( elemRepo.elementTextTrim( "url" ) );
494 repo.setLayout( StringUtils.defaultIfEmpty( elemRepo.elementTextTrim( "layout" ), "default" ) );
495 repo.setPlugins( isPluginRepo );
497 repo.setReleases( toBoolean( xml.getElementText( elemRepo, "releases/enabled" ), true ) );
498 repo.setSnapshots( toBoolean( xml.getElementText( elemRepo, "snapshots/enabled" ), false ) );
506 private Scm getSCM( XMLReader xml )
509 Element elemScm = xml.getElement( "//project/scm" );
511 if ( elemScm != null )
515 scm.setConnection( elemScm.elementTextTrim( "connection" ) );
516 scm.setDeveloperConnection( elemScm.elementTextTrim( "developerConnection" ) );
517 scm.setUrl( elemScm.elementTextTrim( "url" ) );
525 private VersionedReference getVersionedReference( Element elem )
527 VersionedReference reference = new VersionedReference();
529 reference.setGroupId( elem.elementTextTrim( "groupId" ) );
530 reference.setArtifactId( elem.elementTextTrim( "artifactId" ) );
531 reference.setVersion( elem.elementTextTrim( "version" ) );
536 private boolean toBoolean( String value, boolean defaultValue )
538 if ( StringUtils.equalsIgnoreCase( value, "true" ) )
542 else if ( StringUtils.equalsIgnoreCase( value, "false" ) )
548 // If unset, or not "true" or "false".