1 package org.apache.maven.archiva.repository.project;
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 java.util.ArrayList;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.Properties;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.model.ArchivaModelCloner;
34 import org.apache.maven.archiva.model.ArchivaProjectModel;
35 import org.apache.maven.archiva.model.ArtifactReference;
36 import org.apache.maven.archiva.model.CiManagement;
37 import org.apache.maven.archiva.model.Dependency;
38 import org.apache.maven.archiva.model.Exclusion;
39 import org.apache.maven.archiva.model.Individual;
40 import org.apache.maven.archiva.model.IssueManagement;
41 import org.apache.maven.archiva.model.License;
42 import org.apache.maven.archiva.model.Organization;
43 import org.apache.maven.archiva.model.ProjectRepository;
44 import org.apache.maven.archiva.model.Scm;
49 * TODO: Should call this ProjectModelAncestry as it deals with the current project and its parent.
53 public class ProjectModelMerge
56 * Merge the contents of a project with it's parent project.
58 * @param mainProject the main project.
59 * @param parentProject the parent project to merge.
60 * @throws ProjectModelException if there was a problem merging the model.
62 public static ArchivaProjectModel merge( ArchivaProjectModel mainProject, ArchivaProjectModel parentProject )
63 throws ProjectModelException
65 if ( mainProject == null )
67 throw new ProjectModelException( "Cannot merge with a null main project." );
70 if ( parentProject == null )
72 throw new ProjectModelException( "Cannot merge with a null parent project." );
75 ArchivaProjectModel merged = new ArchivaProjectModel();
78 merged.setParentProject(mainProject.getParentProject());
79 merged.setArtifactId( mainProject.getArtifactId() );
80 merged.setPackaging( StringUtils.defaultIfEmpty( mainProject.getPackaging(), "jar" ) );
81 merged.setRelocation( mainProject.getRelocation() );
84 merged.setGroupId( merge( mainProject.getGroupId(), parentProject.getGroupId() ) );
85 merged.setVersion( merge( mainProject.getVersion(), parentProject.getVersion() ) );
86 merged.setName( merge( mainProject.getName(), parentProject.getName() ) );
87 merged.setUrl( merge( mainProject.getUrl(), parentProject.getUrl() ) );
88 merged.setDescription( merge( mainProject.getDescription(), parentProject.getDescription() ) );
90 merged.setOrigin( "merged" );
92 merged.setCiManagement( merge( mainProject.getCiManagement(), parentProject.getCiManagement() ) );
93 merged.setIndividuals( mergeIndividuals( mainProject.getIndividuals(), parentProject.getIndividuals() ) );
94 merged.setIssueManagement( merge( mainProject.getIssueManagement(), parentProject.getIssueManagement() ) );
95 merged.setLicenses( mergeLicenses( mainProject.getLicenses(), parentProject.getLicenses() ) );
96 merged.setOrganization( merge( mainProject.getOrganization(), parentProject.getOrganization() ) );
97 merged.setScm( merge( mainProject.getScm(), parentProject.getScm() ) );
98 merged.setRepositories( mergeRepositories( mainProject.getRepositories(), parentProject.getRepositories() ) );
99 merged.setDependencies( mergeDependencies( mainProject.getDependencies(), parentProject.getDependencies() ) );
100 merged.setDependencyManagement( mergeDependencyManagement( mainProject.getDependencyManagement(), parentProject
101 .getDependencyManagement() ) );
102 merged.setPlugins( mergePlugins( mainProject.getPlugins(), parentProject.getPlugins() ) );
103 merged.setReports( mergeReports( mainProject.getReports(), parentProject.getReports() ) );
104 merged.setProperties( merge( mainProject.getProperties(), parentProject.getProperties() ) );
109 private static Map<String, ArtifactReference> createArtifactReferenceMap( List<ArtifactReference> artifactReferences )
111 Map<String, ArtifactReference> ret = new HashMap<String, ArtifactReference>();
113 for ( ArtifactReference artifactReference : artifactReferences )
115 String key = toVersionlessArtifactKey( artifactReference );
116 ret.put( key, artifactReference );
122 private static Map<String, Dependency> createDependencyMap( List<Dependency> dependencies )
124 Map<String, Dependency> ret = new HashMap<String, Dependency>();
126 Iterator<Dependency> it = dependencies.iterator();
127 while ( it.hasNext() )
129 Dependency dep = it.next();
130 String key = toVersionlessDependencyKey( dep );
137 private static Map<String, Exclusion> createExclusionMap( List<Exclusion> exclusions )
139 Map<String, Exclusion> ret = new HashMap<String, Exclusion>();
141 Iterator<Exclusion> it = exclusions.iterator();
142 while ( it.hasNext() )
144 Exclusion exclusion = it.next();
145 String key = exclusion.getGroupId() + ":" + exclusion.getArtifactId();
146 ret.put( key, exclusion );
152 private static Map<String, License> createLicensesMap( List<License> licenses )
154 Map<String, License> ret = new HashMap<String, License>();
156 for ( License license : licenses )
158 // TODO: Change to 'id' when LicenseTypeMapper is created.
159 String key = license.getName();
160 ret.put( key, license );
166 private static Map<String, ProjectRepository> createRepositoriesMap( List<ProjectRepository> repositories )
168 Map<String, ProjectRepository> ret = new HashMap<String, ProjectRepository>();
170 for ( ProjectRepository repo : repositories )
172 // Should this really be using repo.id ?
173 String key = repo.getUrl();
174 ret.put( key, repo );
180 private static boolean empty( String val )
187 return ( val.trim().length() <= 0 );
190 private static ArtifactReference merge( ArtifactReference mainArtifactReference,
191 ArtifactReference parentArtifactReference )
193 if ( parentArtifactReference == null )
195 return mainArtifactReference;
198 if ( mainArtifactReference == null )
200 return ArchivaModelCloner.clone( parentArtifactReference );
203 ArtifactReference merged = new ArtifactReference();
206 merged.setGroupId( mainArtifactReference.getGroupId() );
207 merged.setArtifactId( mainArtifactReference.getArtifactId() );
210 merged.setVersion( merge( mainArtifactReference.getVersion(), parentArtifactReference.getVersion() ) );
211 merged.setClassifier( merge( mainArtifactReference.getClassifier(), parentArtifactReference.getClassifier() ) );
212 merged.setType( merge( mainArtifactReference.getType(), parentArtifactReference.getType() ) );
217 private static CiManagement merge( CiManagement mainCim, CiManagement parentCim )
219 if ( parentCim == null )
224 if ( mainCim == null )
226 return ArchivaModelCloner.clone( parentCim );
229 CiManagement merged = new CiManagement();
231 merged.setSystem( merge( mainCim.getSystem(), parentCim.getSystem() ) );
232 merged.setUrl( merge( mainCim.getUrl(), parentCim.getUrl() ) );
237 private static Dependency merge( Dependency mainDep, Dependency parentDep )
239 if ( parentDep == null )
244 if ( mainDep == null )
246 Dependency dep = ArchivaModelCloner.clone( parentDep );
247 dep.setFromParent( true );
251 Dependency merged = new Dependency();
253 merged.setFromParent( true );
256 merged.setGroupId( mainDep.getGroupId() );
257 merged.setArtifactId( mainDep.getArtifactId() );
260 merged.setVersion( merge( mainDep.getVersion(), parentDep.getVersion() ) );
261 merged.setClassifier( merge( mainDep.getClassifier(), parentDep.getClassifier() ) );
262 merged.setType( merge( mainDep.getType(), parentDep.getType() ) );
263 merged.setScope( merge( mainDep.getScope(), parentDep.getScope() ) );
264 if ( parentDep.isOptional() )
266 merged.setOptional( true );
269 merged.setSystemPath( merge( mainDep.getSystemPath(), parentDep.getSystemPath() ) );
270 merged.setUrl( merge( mainDep.getUrl(), parentDep.getUrl() ) );
271 merged.setExclusions( mergeExclusions( mainDep.getExclusions(), parentDep.getExclusions() ) );
276 private static IssueManagement merge( IssueManagement mainIssueManagement, IssueManagement parentIssueManagement )
278 if ( parentIssueManagement == null )
280 return mainIssueManagement;
283 if ( mainIssueManagement == null )
285 return ArchivaModelCloner.clone( parentIssueManagement );
288 IssueManagement merged = new IssueManagement();
290 merged.setSystem( merge( mainIssueManagement.getSystem(), parentIssueManagement.getSystem() ) );
291 merged.setUrl( merge( mainIssueManagement.getUrl(), parentIssueManagement.getUrl() ) );
296 private static Organization merge( Organization mainOrganization, Organization parentOrganization )
298 if ( parentOrganization == null )
300 return mainOrganization;
303 if ( mainOrganization == null )
305 return ArchivaModelCloner.clone( parentOrganization );
308 Organization merged = new Organization();
310 merged.setFavicon( merge( mainOrganization.getFavicon(), parentOrganization.getFavicon() ) );
311 merged.setName( merge( mainOrganization.getName(), parentOrganization.getName() ) );
312 merged.setUrl( merge( mainOrganization.getUrl(), parentOrganization.getUrl() ) );
317 @SuppressWarnings("unchecked")
318 private static Properties merge( Properties mainProperties, Properties parentProperties )
320 if ( parentProperties == null )
322 return mainProperties;
325 if ( mainProperties == null )
327 return ArchivaModelCloner.clone( parentProperties );
330 Properties merged = new Properties();
331 merged.putAll(mainProperties);
333 Enumeration<String> keys = (Enumeration<String>) parentProperties.propertyNames();
334 while ( keys.hasMoreElements() )
336 String key = (String) keys.nextElement();
337 merged.put( key, merge( mainProperties.getProperty( key ), parentProperties.getProperty( key ) ) );
343 private static Scm merge( Scm mainScm, Scm parentScm )
345 if ( parentScm == null )
350 if ( mainScm == null )
352 return ArchivaModelCloner.clone( parentScm );
355 Scm merged = new Scm();
357 merged.setConnection( merge( mainScm.getConnection(), parentScm.getConnection() ) );
358 merged.setDeveloperConnection( merge( mainScm.getDeveloperConnection(), parentScm.getDeveloperConnection() ) );
359 merged.setUrl( merge( mainScm.getUrl(), parentScm.getUrl() ) );
364 private static String merge( String main, String parent )
366 if ( empty( main ) && !empty( parent ) )
374 private static List<ArtifactReference> mergeArtifactReferences( List<ArtifactReference> mainArtifactReferences, List<ArtifactReference> parentArtifactReferences )
376 if ( parentArtifactReferences == null )
378 return mainArtifactReferences;
381 if ( mainArtifactReferences == null )
383 return ArchivaModelCloner.cloneArtifactReferences( parentArtifactReferences );
386 List<ArtifactReference> merged = new ArrayList<ArtifactReference>();
388 Map<String, ArtifactReference> mainArtifactReferenceMap = createArtifactReferenceMap( mainArtifactReferences );
389 Map<String, ArtifactReference> parentArtifactReferenceMap = createArtifactReferenceMap( parentArtifactReferences );
391 for ( Map.Entry<String,ArtifactReference> entry : mainArtifactReferenceMap.entrySet() )
393 String key = entry.getKey();
394 ArtifactReference mainArtifactReference = (ArtifactReference) entry.getValue();
395 ArtifactReference parentArtifactReference = parentArtifactReferenceMap.get( key );
397 if ( parentArtifactReference == null )
399 merged.add( mainArtifactReference );
403 // Not merging. Local wins.
404 merged.add( merge( mainArtifactReference, parentArtifactReference ) );
411 private static List<Dependency> mergeDependencies( List<Dependency> mainDependencies, List<Dependency> parentDependencies )
413 if ( parentDependencies == null )
415 return mainDependencies;
418 if ( mainDependencies == null )
420 List<Dependency> merged = ArchivaModelCloner.cloneDependencies( parentDependencies );
421 Iterator<Dependency> it = merged.iterator();
422 while ( it.hasNext() )
424 Dependency dep = it.next();
425 dep.setFromParent( true );
430 List<Dependency> merged = new ArrayList<Dependency>();
432 Map<String, Dependency> mainDepMap = createDependencyMap( mainDependencies );
433 Map<String, Dependency> parentDepMap = createDependencyMap( parentDependencies );
434 Set<String> uniqueKeys = new HashSet<String>();
435 uniqueKeys.addAll( mainDepMap.keySet() );
436 uniqueKeys.addAll( parentDepMap.keySet() );
438 Iterator<String> it = uniqueKeys.iterator();
439 while ( it.hasNext() )
441 String key = it.next();
442 Dependency parentDep = parentDepMap.get( key );
443 Dependency mainDep = mainDepMap.get( key );
445 if ( parentDep == null )
447 // Means there is no parent dep to override main dep.
448 merged.add( mainDep );
452 // Parent dep exists (main doesn't have to).
453 // Merge the parent over the main dep.
454 merged.add( merge( mainDep, parentDep ) );
461 private static List<Dependency> mergeDependencyManagement( List<Dependency> mainDepMgmt, List<Dependency> parentDepMgmt )
463 if ( parentDepMgmt == null )
468 if ( mainDepMgmt == null )
470 List<Dependency> merged = ArchivaModelCloner.cloneDependencies( parentDepMgmt );
471 Iterator<Dependency> it = merged.iterator();
472 while ( it.hasNext() )
474 Dependency dep = it.next();
475 dep.setFromParent( true );
480 List<Dependency> merged = new ArrayList<Dependency>();
482 Map<String, Dependency> mainDepMap = createDependencyMap( mainDepMgmt );
483 Map<String, Dependency> parentDepMap = createDependencyMap( parentDepMgmt );
484 Set<String> uniqueKeys = new HashSet<String>();
485 uniqueKeys.addAll( mainDepMap.keySet() );
486 uniqueKeys.addAll( parentDepMap.keySet() );
488 Iterator<String> it = uniqueKeys.iterator();
489 while ( it.hasNext() )
491 String key = it.next();
492 Dependency parentDep = parentDepMap.get( key );
493 Dependency mainDep = mainDepMap.get( key );
495 if ( parentDep == null )
497 // Means there is no parent depMan entry to override main depMan.
498 merged.add( mainDep );
502 // Parent depMan entry exists (main doesn't have to).
503 // Merge the parent over the main depMan entry.
504 merged.add( merge( mainDep, parentDep ) );
511 public static List<Exclusion> mergeExclusions( List<Exclusion> mainExclusions, List<Exclusion> parentExclusions )
513 if ( parentExclusions == null )
515 return mainExclusions;
518 if ( mainExclusions == null )
520 return ArchivaModelCloner.cloneExclusions( parentExclusions );
523 List<Exclusion> merged = new ArrayList<Exclusion>();
525 Map<String, Exclusion> mainExclusionMap = createExclusionMap( mainExclusions );
526 Map<String, Exclusion> parentExclusionMap = createExclusionMap( parentExclusions );
528 for ( Map.Entry<String, Exclusion> entry : mainExclusionMap.entrySet() )
530 String key = entry.getKey();
531 Exclusion mainExclusion = entry.getValue();
532 Exclusion parentExclusion = parentExclusionMap.get( key );
534 if ( parentExclusion == null )
536 merged.add( mainExclusion );
540 merged.add( parentExclusion );
547 private static List<Individual> mergeIndividuals( List<Individual> mainIndividuals, List<Individual> parentIndividuals )
549 if ( parentIndividuals == null )
551 return mainIndividuals;
554 if ( mainIndividuals == null )
556 return ArchivaModelCloner.cloneIndividuals( parentIndividuals );
559 List<Individual> merged = ArchivaModelCloner.cloneIndividuals( mainIndividuals );
561 Iterator<Individual> it = parentIndividuals.iterator();
562 while ( it.hasNext() )
564 Individual parentIndividual = it.next();
566 if ( !mainIndividuals.contains( parentIndividual ) )
568 merged.add( parentIndividual );
575 private static List<License> mergeLicenses( List<License> mainLicenses, List<License> parentLicenses )
577 if ( parentLicenses == null )
582 if ( mainLicenses == null )
584 return ArchivaModelCloner.cloneLicenses( parentLicenses );
587 List<License> merged = new ArrayList<License>();
589 Map<String, License> mainLicensesMap = createLicensesMap( mainLicenses );
590 Map<String, License> parentLicensesMap = createLicensesMap( parentLicenses );
592 for ( Map.Entry<String, License> entry : mainLicensesMap.entrySet() )
594 String key = entry.getKey();
595 License mainLicense = entry.getValue();
596 License parentLicense = parentLicensesMap.get( key );
598 if ( parentLicense == null )
600 merged.add( mainLicense );
604 // Not merging. Local wins.
605 merged.add( parentLicense );
612 private static List<ArtifactReference> mergePlugins( List<ArtifactReference> mainPlugins, List<ArtifactReference> parentPlugins )
614 return mergeArtifactReferences( mainPlugins, parentPlugins );
617 private static List<ArtifactReference> mergeReports( List<ArtifactReference> mainReports, List<ArtifactReference> parentReports )
619 return mergeArtifactReferences( mainReports, parentReports );
622 private static List<ProjectRepository> mergeRepositories( List<ProjectRepository> mainRepositories, List<ProjectRepository> parentRepositories )
624 if ( parentRepositories == null )
626 return mainRepositories;
629 if ( mainRepositories == null )
631 return ArchivaModelCloner.cloneRepositories( parentRepositories );
634 List<ProjectRepository> merged = new ArrayList<ProjectRepository>();
636 Map<String, ProjectRepository> mainRepositoriesMap = createRepositoriesMap( mainRepositories );
637 Map<String, ProjectRepository> parentRepositoriesMap = createRepositoriesMap( parentRepositories );
639 for ( Map.Entry<String, ProjectRepository> entry : mainRepositoriesMap.entrySet() )
641 String key = entry.getKey();
642 ProjectRepository mainProjectRepository = entry.getValue();
643 ProjectRepository parentProjectRepository = parentRepositoriesMap.get( key );
645 if ( parentProjectRepository == null )
647 merged.add( mainProjectRepository );
651 // Not merging. Local wins.
652 merged.add( parentProjectRepository );
659 private static String toVersionlessArtifactKey( ArtifactReference artifactReference )
661 StringBuffer key = new StringBuffer();
663 key.append( artifactReference.getGroupId() ).append( ":" ).append( artifactReference.getArtifactId() );
664 key.append( StringUtils.defaultString( artifactReference.getClassifier() ) ).append( ":" );
665 key.append( artifactReference.getType() );
667 return key.toString();
670 private static String toVersionlessDependencyKey( Dependency dep )
672 StringBuffer key = new StringBuffer();
674 key.append( dep.getGroupId() ).append( ":" ).append( dep.getArtifactId() );
675 key.append( StringUtils.defaultString( dep.getClassifier() ) ).append( ":" );
676 key.append( dep.getType() );
678 return key.toString();