KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > ModelUtils


1 package org.apache.maven.project;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.model.Activation;
20 import org.apache.maven.model.ActivationFile;
21 import org.apache.maven.model.ActivationProperty;
22 import org.apache.maven.model.Build;
23 import org.apache.maven.model.BuildBase;
24 import org.apache.maven.model.Dependency;
25 import org.apache.maven.model.DependencyManagement;
26 import org.apache.maven.model.DeploymentRepository;
27 import org.apache.maven.model.DistributionManagement;
28 import org.apache.maven.model.Exclusion;
29 import org.apache.maven.model.Extension;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.Parent;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.model.PluginContainer;
34 import org.apache.maven.model.PluginExecution;
35 import org.apache.maven.model.PluginManagement;
36 import org.apache.maven.model.Profile;
37 import org.apache.maven.model.Relocation;
38 import org.apache.maven.model.ReportPlugin;
39 import org.apache.maven.model.ReportSet;
40 import org.apache.maven.model.Reporting;
41 import org.apache.maven.model.Repository;
42 import org.apache.maven.model.RepositoryBase;
43 import org.apache.maven.model.RepositoryPolicy;
44 import org.apache.maven.model.Resource;
45 import org.apache.maven.model.Site;
46 import org.apache.maven.project.inheritance.DefaultModelInheritanceAssembler;
47 import org.apache.maven.project.inheritance.ModelInheritanceAssembler;
48 import org.codehaus.plexus.util.xml.Xpp3Dom;
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Properties JavaDoc;
55 import java.util.TreeMap JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 public final class ModelUtils
59 {
60     public static void mergePluginLists( PluginContainer childContainer, PluginContainer parentContainer,
61                                          boolean handleAsInheritance )
62     {
63         if ( childContainer == null || parentContainer == null )
64         {
65             // nothing to do.
66
return;
67         }
68
69         List JavaDoc mergedPlugins = new ArrayList JavaDoc();
70
71         List JavaDoc parentPlugins = parentContainer.getPlugins();
72
73         if ( parentPlugins != null && !parentPlugins.isEmpty() )
74         {
75             Map JavaDoc assembledPlugins = new TreeMap JavaDoc();
76
77             Map JavaDoc childPlugins = childContainer.getPluginsAsMap();
78
79             for ( Iterator JavaDoc it = parentPlugins.iterator(); it.hasNext(); )
80             {
81                 Plugin parentPlugin = (Plugin) it.next();
82
83                 String JavaDoc parentInherited = parentPlugin.getInherited();
84
85                 if ( !handleAsInheritance || parentInherited == null ||
86                     Boolean.valueOf( parentInherited ).booleanValue() )
87                 {
88
89                     Plugin assembledPlugin = parentPlugin;
90
91                     Plugin childPlugin = (Plugin) childPlugins.get( parentPlugin.getKey() );
92
93                     if ( childPlugin != null )
94                     {
95                         assembledPlugin = childPlugin;
96
97                         mergePluginDefinitions( childPlugin, parentPlugin, handleAsInheritance );
98                     }
99
100                     if ( handleAsInheritance && parentInherited == null )
101                     {
102                         assembledPlugin.unsetInheritanceApplied();
103                     }
104
105                     mergedPlugins.add(assembledPlugin);
106                 }
107             }
108
109             for ( Iterator JavaDoc it = childPlugins.values().iterator(); it.hasNext(); )
110             {
111                 Plugin childPlugin = (Plugin) it.next();
112
113                 if ( !assembledPlugins.containsKey( childPlugin.getKey() ) )
114                 {
115                     mergedPlugins.add(childPlugin);
116                 }
117             }
118
119             childContainer.setPlugins(mergedPlugins);
120
121             childContainer.flushPluginMap();
122         }
123     }
124
125     public static void mergeReportPluginLists( Reporting child, Reporting parent, boolean handleAsInheritance )
126     {
127         if ( child == null || parent == null )
128         {
129             // nothing to do.
130
return;
131         }
132
133         List JavaDoc parentPlugins = parent.getPlugins();
134
135         if ( parentPlugins != null && !parentPlugins.isEmpty() )
136         {
137             Map JavaDoc assembledPlugins = new TreeMap JavaDoc();
138
139             Map JavaDoc childPlugins = child.getReportPluginsAsMap();
140
141             for ( Iterator JavaDoc it = parentPlugins.iterator(); it.hasNext(); )
142             {
143                 ReportPlugin parentPlugin = (ReportPlugin) it.next();
144
145                 String JavaDoc parentInherited = parentPlugin.getInherited();
146
147                 if ( !handleAsInheritance || parentInherited == null ||
148                     Boolean.valueOf( parentInherited ).booleanValue() )
149                 {
150
151                     ReportPlugin assembledPlugin = parentPlugin;
152
153                     ReportPlugin childPlugin = (ReportPlugin) childPlugins.get( parentPlugin.getKey() );
154
155                     if ( childPlugin != null )
156                     {
157                         assembledPlugin = childPlugin;
158
159                         mergeReportPluginDefinitions( childPlugin, parentPlugin, handleAsInheritance );
160                     }
161
162                     if ( handleAsInheritance && parentInherited == null )
163                     {
164                         assembledPlugin.unsetInheritanceApplied();
165                     }
166
167                     assembledPlugins.put( assembledPlugin.getKey(), assembledPlugin );
168                 }
169             }
170
171             for ( Iterator JavaDoc it = childPlugins.values().iterator(); it.hasNext(); )
172             {
173                 ReportPlugin childPlugin = (ReportPlugin) it.next();
174
175                 if ( !assembledPlugins.containsKey( childPlugin.getKey() ) )
176                 {
177                     assembledPlugins.put( childPlugin.getKey(), childPlugin );
178                 }
179             }
180
181             child.setPlugins( new ArrayList JavaDoc( assembledPlugins.values() ) );
182
183             child.flushReportPluginMap();
184         }
185     }
186
187     public static void mergePluginDefinitions( Plugin child, Plugin parent, boolean handleAsInheritance )
188     {
189         if ( child == null || parent == null )
190         {
191             // nothing to do.
192
return;
193         }
194
195         if ( parent.isExtensions() )
196         {
197             child.setExtensions( true );
198         }
199
200         if ( child.getVersion() == null && parent.getVersion() != null )
201         {
202             child.setVersion( parent.getVersion() );
203         }
204
205         Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration();
206         Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration();
207
208         childConfiguration = Xpp3Dom.mergeXpp3Dom( childConfiguration, parentConfiguration );
209
210         child.setConfiguration( childConfiguration );
211
212         child.setDependencies( mergeDependencyList( child.getDependencies(), parent.getDependencies() ) );
213
214         // from here to the end of the method is dealing with merging of the <executions/> section.
215
String JavaDoc parentInherited = parent.getInherited();
216
217         boolean parentIsInherited = parentInherited == null || Boolean.valueOf( parentInherited ).booleanValue();
218
219         List JavaDoc parentExecutions = parent.getExecutions();
220
221         if ( parentExecutions != null && !parentExecutions.isEmpty() )
222         {
223             List JavaDoc mergedExecutions = new ArrayList JavaDoc();
224             
225             Map JavaDoc assembledExecutions = new TreeMap JavaDoc();
226
227             Map JavaDoc childExecutions = child.getExecutionsAsMap();
228
229             for ( Iterator JavaDoc it = parentExecutions.iterator(); it.hasNext(); )
230             {
231                 PluginExecution parentExecution = (PluginExecution) it.next();
232
233                 if ( !handleAsInheritance || parentIsInherited )
234                 {
235                     PluginExecution assembled = parentExecution;
236
237                     PluginExecution childExecution = (PluginExecution) childExecutions.get( parentExecution.getId() );
238
239                     if ( childExecution != null )
240                     {
241                         mergePluginExecutionDefinitions( childExecution, parentExecution );
242
243                         assembled = childExecution;
244                     }
245                     else if ( handleAsInheritance && parentInherited == null )
246                     {
247                         parentExecution.unsetInheritanceApplied();
248                     }
249
250                     assembledExecutions.put( assembled.getId(), assembled );
251                     mergedExecutions.add(assembled);
252                 }
253             }
254
255             for ( Iterator JavaDoc it = child.getExecutions().iterator(); it.hasNext(); )
256             {
257                 PluginExecution childExecution = (PluginExecution)it.next();
258
259                 if ( !assembledExecutions.containsKey( childExecution.getId() ) )
260                 {
261                     mergedExecutions.add(childExecution);
262                 }
263             }
264
265             child.setExecutions(mergedExecutions);
266
267             child.flushExecutionMap();
268         }
269
270     }
271
272     public static void mergeReportPluginDefinitions( ReportPlugin child, ReportPlugin parent,
273                                                      boolean handleAsInheritance )
274     {
275         if ( child == null || parent == null )
276         {
277             // nothing to do.
278
return;
279         }
280
281         if ( child.getVersion() == null && parent.getVersion() != null )
282         {
283             child.setVersion( parent.getVersion() );
284         }
285
286         // from here to the end of the method is dealing with merging of the <executions/> section.
287
String JavaDoc parentInherited = parent.getInherited();
288
289         boolean parentIsInherited = parentInherited == null || Boolean.valueOf( parentInherited ).booleanValue();
290
291         List JavaDoc parentReportSets = parent.getReportSets();
292
293         if ( parentReportSets != null && !parentReportSets.isEmpty() )
294         {
295             Map JavaDoc assembledReportSets = new TreeMap JavaDoc();
296
297             Map JavaDoc childReportSets = child.getReportSetsAsMap();
298
299             for ( Iterator JavaDoc it = parentReportSets.iterator(); it.hasNext(); )
300             {
301                 ReportSet parentReportSet = (ReportSet) it.next();
302
303                 if ( !handleAsInheritance || parentIsInherited )
304                 {
305                     ReportSet assembledReportSet = parentReportSet;
306
307                     ReportSet childReportSet = (ReportSet) childReportSets.get( parentReportSet.getId() );
308
309                     if ( childReportSet != null )
310                     {
311                         mergeReportSetDefinitions( childReportSet, parentReportSet );
312
313                         assembledReportSet = childReportSet;
314                     }
315                     else if ( handleAsInheritance && parentInherited == null )
316                     {
317                         parentReportSet.unsetInheritanceApplied();
318                     }
319
320                     assembledReportSets.put( assembledReportSet.getId(), assembledReportSet );
321                 }
322             }
323
324             for ( Iterator JavaDoc it = childReportSets.entrySet().iterator(); it.hasNext(); )
325             {
326                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
327
328                 String JavaDoc id = (String JavaDoc) entry.getKey();
329
330                 if ( !assembledReportSets.containsKey( id ) )
331                 {
332                     assembledReportSets.put( id, entry.getValue() );
333                 }
334             }
335
336             child.setReportSets( new ArrayList JavaDoc( assembledReportSets.values() ) );
337
338             child.flushReportSetMap();
339         }
340
341     }
342
343     private static void mergePluginExecutionDefinitions( PluginExecution child, PluginExecution parent )
344     {
345         if ( child.getPhase() == null )
346         {
347             child.setPhase( parent.getPhase() );
348         }
349
350         List JavaDoc parentGoals = parent.getGoals();
351         List JavaDoc childGoals = child.getGoals();
352
353         List JavaDoc goals = new ArrayList JavaDoc();
354
355         if ( childGoals != null && !childGoals.isEmpty() )
356         {
357             goals.addAll( childGoals );
358         }
359
360         if ( parentGoals != null )
361         {
362             for ( Iterator JavaDoc goalIterator = parentGoals.iterator(); goalIterator.hasNext(); )
363             {
364                 String JavaDoc goal = (String JavaDoc) goalIterator.next();
365
366                 if ( !goals.contains( goal ) )
367                 {
368                     goals.add( goal );
369                 }
370             }
371         }
372
373         child.setGoals( goals );
374
375         Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration();
376         Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration();
377
378         childConfiguration = Xpp3Dom.mergeXpp3Dom( childConfiguration, parentConfiguration );
379
380         child.setConfiguration( childConfiguration );
381     }
382
383     private static void mergeReportSetDefinitions( ReportSet child, ReportSet parent )
384     {
385         List JavaDoc parentReports = parent.getReports();
386         List JavaDoc childReports = child.getReports();
387
388         List JavaDoc reports = new ArrayList JavaDoc();
389
390         if ( childReports != null && !childReports.isEmpty() )
391         {
392             reports.addAll( childReports );
393         }
394
395         if ( parentReports != null )
396         {
397             for ( Iterator JavaDoc i = parentReports.iterator(); i.hasNext(); )
398             {
399                 String JavaDoc report = (String JavaDoc) i.next();
400
401                 if ( !reports.contains( report ) )
402                 {
403                     reports.add( report );
404                 }
405             }
406         }
407
408         child.setReports( reports );
409
410         Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration();
411         Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration();
412
413         childConfiguration = Xpp3Dom.mergeXpp3Dom( childConfiguration, parentConfiguration );
414
415         child.setConfiguration( childConfiguration );
416     }
417
418     public static Model cloneModel( Model model )
419     {
420         // TODO: would be nice for the modello:java code to generate this as a copy constructor
421
Model newModel = new Model();
422         ModelInheritanceAssembler assembler = new DefaultModelInheritanceAssembler();
423         newModel.setModelVersion( model.getModelVersion() );
424         newModel.setName( model.getName() );
425         newModel.setParent( cloneParent( model.getParent() ) );
426         newModel.setVersion( model.getVersion() );
427         newModel.setArtifactId( model.getArtifactId() );
428         newModel.setProperties( new Properties JavaDoc( model.getProperties() ) );
429         newModel.setGroupId( model.getGroupId() );
430         newModel.setPackaging( model.getPackaging() );
431         newModel.setModules( cloneModules( model.getModules() ) );
432
433         newModel.setProfiles( cloneProfiles( model.getProfiles() ) );
434
435         assembler.copyModel( newModel, model );
436
437         return newModel;
438     }
439
440     private static List JavaDoc cloneProfiles( List JavaDoc profiles )
441     {
442         if ( profiles == null )
443         {
444             return profiles;
445         }
446
447         List JavaDoc newProfiles = new ArrayList JavaDoc( profiles.size() );
448
449         for ( Iterator JavaDoc it = profiles.iterator(); it.hasNext(); )
450         {
451             Profile profile = (Profile) it.next();
452
453             Profile newProfile = new Profile();
454
455             newProfile.setId( profile.getId() );
456
457             newProfile.setActivation( cloneProfileActivation( profile.getActivation() ) );
458
459             newProfile.setBuild( cloneProfileBuild( profile.getBuild() ) );
460
461             newProfile.setDependencies( cloneProfileDependencies( profile.getDependencies() ) );
462
463             DependencyManagement dm = profile.getDependencyManagement();
464
465             if ( dm != null )
466             {
467                 DependencyManagement newDM = new DependencyManagement();
468
469                 newDM.setDependencies( cloneProfileDependencies( dm.getDependencies() ) );
470
471                 newProfile.setDependencyManagement( newDM );
472             }
473
474             newProfile.setDistributionManagement( cloneProfileDistributionManagement( profile
475                 .getDistributionManagement() ) );
476
477             List JavaDoc modules = profile.getModules();
478
479             if ( modules != null && !modules.isEmpty() )
480             {
481                 newProfile.setModules( new ArrayList JavaDoc( modules ) );
482             }
483
484             newProfile.setPluginRepositories( cloneProfileRepositories( profile.getPluginRepositories() ) );
485
486             Properties JavaDoc props = profile.getProperties();
487
488             if ( props != null )
489             {
490                 Properties JavaDoc newProps = new Properties JavaDoc();
491                 newProps.putAll( props );
492
493                 newProfile.setProperties( newProps );
494             }
495
496             newProfile.setReporting( cloneProfileReporting( profile.getReporting() ) );
497
498             newProfile.setReports( profile.getReports() );
499
500             newProfile.setRepositories( cloneProfileRepositories( profile.getRepositories() ) );
501
502             newProfile.setSource( profile.getSource() );
503
504             newProfiles.add( newProfile );
505         }
506
507         return newProfiles;
508     }
509
510     private static Reporting cloneProfileReporting( Reporting reporting )
511     {
512         Reporting newR = null;
513
514         if ( reporting != null )
515         {
516             newR = new Reporting();
517
518             newR.setOutputDirectory( reporting.getOutputDirectory() );
519
520             List JavaDoc plugins = reporting.getPlugins();
521
522             if ( plugins != null )
523             {
524                 List JavaDoc newP = new ArrayList JavaDoc( plugins.size() );
525
526                 for ( Iterator JavaDoc it = plugins.iterator(); it.hasNext(); )
527                 {
528                     ReportPlugin plugin = (ReportPlugin) it.next();
529
530                     ReportPlugin newPlugin = new ReportPlugin();
531
532                     newPlugin.setArtifactId( plugin.getArtifactId() );
533                     newPlugin.setGroupId( plugin.getGroupId() );
534                     newPlugin.setVersion( plugin.getVersion() );
535                     newPlugin.setInherited( plugin.getInherited() );
536                     newPlugin.setReportSets( cloneReportSets( plugin.getReportSets() ) );
537
538                     // TODO: Implement deep-copy of configuration.
539
newPlugin.setConfiguration( plugin.getConfiguration() );
540
541                     newP.add( newPlugin );
542                 }
543
544                 newR.setPlugins( newP );
545             }
546         }
547
548         return newR;
549     }
550
551     private static List JavaDoc cloneReportSets( List JavaDoc sets )
552     {
553         List JavaDoc newSets = null;
554
555         if ( sets != null )
556         {
557             newSets = new ArrayList JavaDoc( sets.size() );
558
559             for ( Iterator JavaDoc it = sets.iterator(); it.hasNext(); )
560             {
561                 ReportSet set = (ReportSet) it.next();
562
563                 ReportSet newSet = new ReportSet();
564
565                 // TODO: Deep-copy config.
566
newSet.setConfiguration( set.getConfiguration() );
567
568                 newSet.setId( set.getId() );
569                 newSet.setInherited( set.getInherited() );
570
571                 newSet.setReports( new ArrayList JavaDoc( set.getReports() ) );
572
573                 newSets.add( newSet );
574             }
575         }
576
577         return newSets;
578     }
579
580     private static List JavaDoc cloneProfileRepositories( List JavaDoc repos )
581     {
582         List JavaDoc newRepos = null;
583
584         if ( repos != null )
585         {
586             newRepos = new ArrayList JavaDoc( repos.size() );
587
588             for ( Iterator JavaDoc it = repos.iterator(); it.hasNext(); )
589             {
590                 Repository repo = (Repository) it.next();
591
592                 Repository newRepo = new Repository();
593
594                 newRepo.setId( repo.getId() );
595                 newRepo.setLayout( repo.getLayout() );
596                 newRepo.setName( repo.getName() );
597
598                 RepositoryPolicy releasePolicy = repo.getReleases();
599
600                 if ( releasePolicy != null )
601                 {
602                     RepositoryPolicy newPolicy = new RepositoryPolicy();
603                     newPolicy.setEnabled( releasePolicy.isEnabled() );
604                     newPolicy.setChecksumPolicy( releasePolicy.getChecksumPolicy() );
605                     newPolicy.setUpdatePolicy( releasePolicy.getUpdatePolicy() );
606
607                     newRepo.setReleases( newPolicy );
608                 }
609
610                 RepositoryPolicy snapPolicy = repo.getSnapshots();
611
612                 if ( snapPolicy != null )
613                 {
614                     RepositoryPolicy newPolicy = new RepositoryPolicy();
615                     newPolicy.setEnabled( snapPolicy.isEnabled() );
616                     newPolicy.setChecksumPolicy( snapPolicy.getChecksumPolicy() );
617                     newPolicy.setUpdatePolicy( snapPolicy.getUpdatePolicy() );
618
619                     newRepo.setSnapshots( newPolicy );
620                 }
621
622                 newRepo.setUrl( repo.getUrl() );
623
624                 newRepos.add( newRepo );
625             }
626         }
627
628         return newRepos;
629     }
630
631     private static DistributionManagement cloneProfileDistributionManagement( DistributionManagement dm )
632     {
633         DistributionManagement newDM = null;
634
635         if ( dm != null )
636         {
637             newDM = new DistributionManagement();
638
639             newDM.setDownloadUrl( dm.getDownloadUrl() );
640             newDM.setStatus( dm.getStatus() );
641
642             Relocation relocation = dm.getRelocation();
643
644             if ( relocation != null )
645             {
646                 Relocation newR = new Relocation();
647
648                 newR.setArtifactId( relocation.getArtifactId() );
649                 newR.setGroupId( relocation.getGroupId() );
650                 newR.setMessage( relocation.getMessage() );
651                 newR.setVersion( relocation.getVersion() );
652
653                 newDM.setRelocation( newR );
654             }
655
656             RepositoryBase repo = dm.getRepository();
657
658             if ( repo != null )
659             {
660                 DeploymentRepository newRepo = new DeploymentRepository();
661
662                 newRepo.setId( repo.getId() );
663                 newRepo.setLayout( repo.getLayout() );
664                 newRepo.setName( repo.getName() );
665                 newRepo.setUrl( repo.getUrl() );
666
667                 newDM.setRepository( newRepo );
668             }
669
670             Site site = dm.getSite();
671
672             if ( site != null )
673             {
674                 Site newSite = new Site();
675
676                 newSite.setId( site.getId() );
677                 newSite.setName( site.getName() );
678                 newSite.setUrl( site.getUrl() );
679
680                 newDM.setSite( newSite );
681             }
682
683             RepositoryBase sRepo = dm.getSnapshotRepository();
684
685             if ( sRepo != null )
686             {
687                 DeploymentRepository newRepo = new DeploymentRepository();
688
689                 newRepo.setId( sRepo.getId() );
690                 newRepo.setLayout( sRepo.getLayout() );
691                 newRepo.setName( sRepo.getName() );
692                 newRepo.setUrl( sRepo.getUrl() );
693
694                 newDM.setSnapshotRepository( newRepo );
695             }
696         }
697
698         return newDM;
699     }
700
701     private static List JavaDoc cloneProfileDependencies( List JavaDoc dependencies )
702     {
703         List JavaDoc newDependencies = null;
704
705         if ( dependencies != null )
706         {
707             newDependencies = new ArrayList JavaDoc( dependencies.size() );
708
709             for ( Iterator JavaDoc it = dependencies.iterator(); it.hasNext(); )
710             {
711                 Dependency dep = (Dependency) it.next();
712
713                 Dependency newDep = new Dependency();
714
715                 newDep.setArtifactId( dep.getArtifactId() );
716                 newDep.setClassifier( dep.getClassifier() );
717                 newDep.setExclusions( cloneDependencyExclusions( dep.getExclusions() ) );
718                 newDep.setGroupId( dep.getGroupId() );
719                 newDep.setScope( dep.getScope() );
720                 newDep.setSystemPath( dep.getSystemPath() );
721                 newDep.setType( dep.getType() );
722                 newDep.setVersion( dep.getVersion() );
723
724                 newDependencies.add( newDep );
725             }
726         }
727
728         return newDependencies;
729     }
730
731     private static List JavaDoc cloneDependencyExclusions( List JavaDoc ex )
732     {
733         List JavaDoc newEx = null;
734
735         if ( ex != null )
736         {
737             newEx = new ArrayList JavaDoc( ex.size() );
738
739             for ( Iterator JavaDoc it = ex.iterator(); it.hasNext(); )
740             {
741                 Exclusion exclusion = (Exclusion) it.next();
742
743                 Exclusion newExclusion = new Exclusion();
744
745                 newExclusion.setArtifactId( exclusion.getArtifactId() );
746                 newExclusion.setGroupId( exclusion.getGroupId() );
747
748                 newEx.add( newExclusion );
749             }
750         }
751
752         return newEx;
753     }
754
755     private static BuildBase cloneProfileBuild( BuildBase build )
756     {
757         BuildBase newBuild = null;
758         if ( build != null )
759         {
760             newBuild = new BuildBase();
761
762             newBuild.setDefaultGoal( build.getDefaultGoal() );
763             newBuild.setDirectory( build.getDirectory() );
764             newBuild.setFinalName( build.getFinalName() );
765
766             newBuild.setPluginManagement( cloneProfilePluginManagement( build.getPluginManagement() ) );
767             newBuild.setPlugins( cloneProfilePlugins( build.getPlugins() ) );
768             newBuild.setResources( cloneProfileResources( build.getResources() ) );
769             newBuild.setTestResources( cloneProfileResources( build.getTestResources() ) );
770         }
771
772         return newBuild;
773     }
774
775     private static List JavaDoc cloneProfileResources( List JavaDoc resources )
776     {
777         List JavaDoc newResources = null;
778
779         if ( resources != null )
780         {
781             newResources = new ArrayList JavaDoc( resources.size() );
782
783             for ( Iterator JavaDoc it = resources.iterator(); it.hasNext(); )
784             {
785                 Resource resource = (Resource) it.next();
786
787                 Resource newResource = new Resource();
788
789                 newResource.setDirectory( resource.getDirectory() );
790                 newResource.setExcludes( new ArrayList JavaDoc( resource.getExcludes() ) );
791                 newResource.setFiltering( resource.isFiltering() );
792                 newResource.setIncludes( new ArrayList JavaDoc( resource.getIncludes() ) );
793                 newResource.setTargetPath( resource.getTargetPath() );
794
795                 newResources.add( newResource );
796             }
797         }
798
799         return newResources;
800     }
801
802     private static PluginManagement cloneProfilePluginManagement( PluginManagement pluginManagement )
803     {
804         PluginManagement newPM = null;
805
806         if ( pluginManagement != null )
807         {
808             newPM = new PluginManagement();
809
810             List JavaDoc plugins = pluginManagement.getPlugins();
811
812             newPM.setPlugins( cloneProfilePlugins( plugins ) );
813         }
814
815         return newPM;
816     }
817
818     private static List JavaDoc cloneProfilePlugins( List JavaDoc plugins )
819     {
820         List JavaDoc newPlugins = null;
821
822         if ( plugins != null )
823         {
824             newPlugins = new ArrayList JavaDoc( plugins.size() );
825
826             for ( Iterator JavaDoc it = plugins.iterator(); it.hasNext(); )
827             {
828                 Plugin plugin = (Plugin) it.next();
829
830                 Plugin newPlugin = new Plugin();
831
832                 newPlugin.setArtifactId( plugin.getArtifactId() );
833                 newPlugin.setExtensions( plugin.isExtensions() );
834                 newPlugin.setGroupId( plugin.getGroupId() );
835                 newPlugin.setInherited( plugin.getInherited() );
836                 newPlugin.setVersion( plugin.getVersion() );
837
838                 // TODO: Deep-copy this!
839
newPlugin.setConfiguration( plugin.getConfiguration() );
840
841                 newPlugin.setExecutions( cloneExecutions( plugin.getExecutions() ) );
842
843                 newPlugins.add( newPlugin );
844             }
845         }
846
847         return newPlugins;
848     }
849
850     private static List JavaDoc cloneExecutions( List JavaDoc executions )
851     {
852         List JavaDoc newExecs = null;
853
854         if ( executions != null )
855         {
856             newExecs = new ArrayList JavaDoc( executions.size() );
857
858             for ( Iterator JavaDoc it = executions.iterator(); it.hasNext(); )
859             {
860                 PluginExecution exec = (PluginExecution) it.next();
861
862                 PluginExecution newExec = new PluginExecution();
863
864                 // TODO: Deep-copy configs.
865
newExec.setConfiguration( exec.getConfiguration() );
866
867                 newExec.setId( exec.getId() );
868                 newExec.setInherited( exec.getInherited() );
869                 newExec.setPhase( exec.getPhase() );
870
871                 List JavaDoc goals = exec.getGoals();
872
873                 if ( goals != null && !goals.isEmpty() )
874                 {
875                     newExec.setGoals( new ArrayList JavaDoc( goals ) );
876                 }
877
878                 newExecs.add( newExec );
879             }
880         }
881
882         return newExecs;
883     }
884
885     private static Activation cloneProfileActivation( Activation activation )
886     {
887         Activation newActivation = null;
888         if ( activation != null )
889         {
890             newActivation = new Activation();
891
892             newActivation.setActiveByDefault( activation.isActiveByDefault() );
893
894             ActivationFile af = activation.getFile();
895
896             if ( af != null )
897             {
898                 ActivationFile afNew = new ActivationFile();
899                 afNew.setExists( af.getExists() );
900                 afNew.setMissing( af.getMissing() );
901
902                 newActivation.setFile( afNew );
903             }
904
905             newActivation.setJdk( activation.getJdk() );
906
907             ActivationProperty ap = activation.getProperty();
908
909             if ( ap != null )
910             {
911                 ActivationProperty newAp = new ActivationProperty();
912
913                 newAp.setName( ap.getName() );
914                 newAp.setValue( ap.getValue() );
915
916                 newActivation.setProperty( newAp );
917             }
918         }
919
920         return newActivation;
921     }
922
923     private static List JavaDoc cloneModules( List JavaDoc modules )
924     {
925         if ( modules == null )
926         {
927             return modules;
928         }
929         return new ArrayList JavaDoc( modules );
930     }
931
932     private static Parent cloneParent( Parent parent )
933     {
934         if ( parent == null )
935         {
936             return parent;
937         }
938
939         Parent newParent = new Parent();
940         newParent.setArtifactId( parent.getArtifactId() );
941         newParent.setGroupId( parent.getGroupId() );
942         newParent.setRelativePath( parent.getRelativePath() );
943         newParent.setVersion( parent.getVersion() );
944         return newParent;
945     }
946
947     public static List JavaDoc mergeRepositoryLists( List JavaDoc dominant, List JavaDoc recessive )
948     {
949         List JavaDoc repositories = new ArrayList JavaDoc();
950
951         for ( Iterator JavaDoc it = dominant.iterator(); it.hasNext(); )
952         {
953             Repository repository = (Repository) it.next();
954
955             repositories.add( repository );
956         }
957
958         for ( Iterator JavaDoc it = recessive.iterator(); it.hasNext(); )
959         {
960             Repository repository = (Repository) it.next();
961
962             if ( !repositories.contains( repository ) )
963             {
964                 repositories.add( repository );
965             }
966         }
967
968         return repositories;
969     }
970
971     public static void mergeExtensionLists( Build childBuild, Build parentBuild )
972     {
973         for ( Iterator JavaDoc i = parentBuild.getExtensions().iterator(); i.hasNext(); )
974         {
975             Extension e = (Extension) i.next();
976             if ( !childBuild.getExtensions().contains( e ) )
977             {
978                 childBuild.addExtension( e );
979             }
980         }
981     }
982
983     public static void mergeResourceLists( List JavaDoc childResources, List JavaDoc parentResources )
984     {
985         for ( Iterator JavaDoc i = parentResources.iterator(); i.hasNext(); )
986         {
987             Resource r = (Resource) i.next();
988             if ( !childResources.contains( r ) )
989             {
990                 childResources.add( r );
991             }
992         }
993     }
994
995     public static void mergeFilterLists( List JavaDoc childFilters, List JavaDoc parentFilters )
996     {
997         for ( Iterator JavaDoc i = parentFilters.iterator(); i.hasNext(); )
998         {
999             String JavaDoc f = (String JavaDoc) i.next();
1000            if ( !childFilters.contains( f ) )
1001            {
1002                childFilters.add( f );
1003            }
1004        }
1005    }
1006
1007    public static List JavaDoc mergeDependencyList( List JavaDoc child, List JavaDoc parent )
1008    {
1009        Map JavaDoc depsMap = new HashMap JavaDoc();
1010
1011        if ( parent != null )
1012        {
1013            for ( Iterator JavaDoc it = parent.iterator(); it.hasNext(); )
1014            {
1015                Dependency dependency = (Dependency) it.next();
1016                depsMap.put( dependency.getManagementKey(), dependency );
1017            }
1018        }
1019
1020        if ( child != null )
1021        {
1022            for ( Iterator JavaDoc it = child.iterator(); it.hasNext(); )
1023            {
1024                Dependency dependency = (Dependency) it.next();
1025                depsMap.put( dependency.getManagementKey(), dependency );
1026            }
1027        }
1028
1029        return new ArrayList JavaDoc( depsMap.values() );
1030    }
1031
1032}
1033
Popular Tags