KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > inheritance > DefaultModelInheritanceAssembler


1 package org.apache.maven.project.inheritance;
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.Build;
20 import org.apache.maven.model.Dependency;
21 import org.apache.maven.model.DependencyManagement;
22 import org.apache.maven.model.DeploymentRepository;
23 import org.apache.maven.model.DistributionManagement;
24 import org.apache.maven.model.Model;
25 import org.apache.maven.model.PluginManagement;
26 import org.apache.maven.model.Reporting;
27 import org.apache.maven.model.Scm;
28 import org.apache.maven.model.Site;
29 import org.apache.maven.project.ModelUtils;
30 import org.codehaus.plexus.util.StringUtils;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.util.TreeMap JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
44  * @version $Id: DefaultModelInheritanceAssembler.java,v 1.4 2004/08/23 20:24:54
45  * jdcasey Exp $
46  * @todo generate this with modello to keep it in sync with changes in the model.
47  */

48 public class DefaultModelInheritanceAssembler
49     implements ModelInheritanceAssembler
50 {
51     public void copyModel( Model dest, Model source )
52     {
53         assembleModelInheritance( dest, source, null, false );
54     }
55
56     public void assembleModelInheritance( Model child, Model parent, String JavaDoc childPathAdjustment )
57     {
58         assembleModelInheritance( child, parent, childPathAdjustment, true );
59     }
60
61     public void assembleModelInheritance( Model child, Model parent )
62     {
63         assembleModelInheritance( child, parent, null, true );
64     }
65
66     private void assembleModelInheritance( Model child, Model parent, String JavaDoc childPathAdjustment, boolean appendPaths )
67     {
68         // cannot inherit from null parent.
69
if ( parent == null )
70         {
71             return;
72         }
73
74         // Group id
75
if ( child.getGroupId() == null )
76         {
77             child.setGroupId( parent.getGroupId() );
78         }
79
80         // version
81
if ( child.getVersion() == null )
82         {
83             // The parent version may have resolved to something different, so we take what we asked for...
84
// instead of - child.setVersion( parent.getVersion() );
85

86             if ( child.getParent() != null )
87             {
88                 child.setVersion( child.getParent().getVersion() );
89             }
90         }
91
92         // inceptionYear
93
if ( child.getInceptionYear() == null )
94         {
95             child.setInceptionYear( parent.getInceptionYear() );
96         }
97
98         // url
99
if ( child.getUrl() == null )
100         {
101             if ( parent.getUrl() != null )
102             {
103                 child.setUrl( appendPath( parent.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
104             }
105             else
106             {
107                 child.setUrl( parent.getUrl() );
108             }
109         }
110
111         assembleDistributionInheritence( child, parent, appendPaths );
112
113         // issueManagement
114
if ( child.getIssueManagement() == null )
115         {
116             child.setIssueManagement( parent.getIssueManagement() );
117         }
118
119         // description
120
if ( child.getDescription() == null )
121         {
122             child.setDescription( parent.getDescription() );
123         }
124
125         // Organization
126
if ( child.getOrganization() == null )
127         {
128             child.setOrganization( parent.getOrganization() );
129         }
130
131         // Scm
132
assembleScmInheritance( child, parent, childPathAdjustment, appendPaths );
133
134         // ciManagement
135
if ( child.getCiManagement() == null )
136         {
137             child.setCiManagement( parent.getCiManagement() );
138         }
139
140         // developers
141
if ( child.getDevelopers().size() == 0 )
142         {
143             child.setDevelopers( parent.getDevelopers() );
144         }
145
146         // licenses
147
if ( child.getLicenses().size() == 0 )
148         {
149             child.setLicenses( parent.getLicenses() );
150         }
151
152         // developers
153
if ( child.getContributors().size() == 0 )
154         {
155             child.setContributors( parent.getContributors() );
156         }
157
158         // mailingLists
159
if ( child.getMailingLists().size() == 0 )
160         {
161             child.setMailingLists( parent.getMailingLists() );
162         }
163
164         // Build
165
assembleBuildInheritance( child, parent );
166
167         assembleDependencyInheritance( child, parent );
168
169         child.setRepositories( ModelUtils.mergeRepositoryLists( child.getRepositories(), parent.getRepositories() ) );
170         child.setPluginRepositories(
171             ModelUtils.mergeRepositoryLists( child.getPluginRepositories(), parent.getPluginRepositories() ) );
172
173         assembleReportingInheritance( child, parent );
174
175         assembleDependencyManagementInheritance( child, parent );
176
177         Properties JavaDoc props = new Properties JavaDoc();
178         props.putAll( parent.getProperties() );
179         props.putAll( child.getProperties() );
180
181         child.setProperties( props );
182     }
183
184     private void assembleDependencyManagementInheritance( Model child, Model parent )
185     {
186         DependencyManagement parentDepMgmt = parent.getDependencyManagement();
187
188         DependencyManagement childDepMgmt = child.getDependencyManagement();
189
190         if ( parentDepMgmt != null )
191         {
192             if ( childDepMgmt == null )
193             {
194                 child.setDependencyManagement( parentDepMgmt );
195             }
196             else
197             {
198                 List JavaDoc childDeps = childDepMgmt.getDependencies();
199
200                 Map JavaDoc mappedChildDeps = new TreeMap JavaDoc();
201                 for ( Iterator JavaDoc it = childDeps.iterator(); it.hasNext(); )
202                 {
203                     Dependency dep = (Dependency) it.next();
204                     mappedChildDeps.put( dep.getManagementKey(), dep );
205                 }
206
207                 for ( Iterator JavaDoc it = parentDepMgmt.getDependencies().iterator(); it.hasNext(); )
208                 {
209                     Dependency dep = (Dependency) it.next();
210                     if ( !mappedChildDeps.containsKey( dep.getManagementKey() ) )
211                     {
212                         childDepMgmt.addDependency( dep );
213                     }
214                 }
215             }
216         }
217     }
218
219     private void assembleReportingInheritance( Model child, Model parent )
220     {
221         // Reports :: aggregate
222
Reporting childReporting = child.getReporting();
223         Reporting parentReporting = parent.getReporting();
224
225         if ( parentReporting != null )
226         {
227             if ( childReporting == null )
228             {
229                 childReporting = new Reporting();
230                 child.setReporting( childReporting );
231             }
232
233             childReporting.setExcludeDefaults( parentReporting.isExcludeDefaults() );
234
235             if ( StringUtils.isEmpty( childReporting.getOutputDirectory() ) )
236             {
237                 childReporting.setOutputDirectory( parentReporting.getOutputDirectory() );
238             }
239
240             ModelUtils.mergeReportPluginLists( childReporting, parentReporting, true );
241         }
242     }
243
244     private void assembleDependencyInheritance( Model child, Model parent )
245     {
246         Map JavaDoc depsMap = new HashMap JavaDoc();
247
248         List JavaDoc deps = parent.getDependencies();
249
250         if ( deps != null )
251         {
252             for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
253             {
254                 Dependency dependency = (Dependency) it.next();
255                 depsMap.put( dependency.getManagementKey(), dependency );
256             }
257         }
258
259         deps = child.getDependencies();
260
261         if ( deps != null )
262         {
263             for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
264             {
265                 Dependency dependency = (Dependency) it.next();
266                 depsMap.put( dependency.getManagementKey(), dependency );
267             }
268         }
269
270         child.setDependencies( new ArrayList JavaDoc( depsMap.values() ) );
271     }
272
273     private void assembleBuildInheritance( Model child, Model parent )
274     {
275         Build childBuild = child.getBuild();
276         Build parentBuild = parent.getBuild();
277
278         if ( parentBuild != null )
279         {
280             if ( childBuild == null )
281             {
282                 childBuild = new Build();
283                 child.setBuild( childBuild );
284             }
285
286             // The build has been set but we want to step in here and fill in
287
// values that have not been set by the child.
288

289             if ( childBuild.getSourceDirectory() == null )
290             {
291                 childBuild.setSourceDirectory( parentBuild.getSourceDirectory() );
292             }
293
294             if ( childBuild.getScriptSourceDirectory() == null )
295             {
296                 childBuild.setScriptSourceDirectory( parentBuild.getScriptSourceDirectory() );
297             }
298
299             if ( childBuild.getTestSourceDirectory() == null )
300             {
301                 childBuild.setTestSourceDirectory( parentBuild.getTestSourceDirectory() );
302             }
303
304             if ( childBuild.getOutputDirectory() == null )
305             {
306                 childBuild.setOutputDirectory( parentBuild.getOutputDirectory() );
307             }
308
309             if ( childBuild.getTestOutputDirectory() == null )
310             {
311                 childBuild.setTestOutputDirectory( parentBuild.getTestOutputDirectory() );
312             }
313
314             // Extensions are accumlated
315
ModelUtils.mergeExtensionLists( childBuild, parentBuild );
316
317             if ( childBuild.getDirectory() == null )
318             {
319                 childBuild.setDirectory( parentBuild.getDirectory() );
320             }
321
322             if ( childBuild.getDefaultGoal() == null )
323             {
324                 childBuild.setDefaultGoal( parentBuild.getDefaultGoal() );
325             }
326
327             if ( childBuild.getFinalName() == null )
328             {
329                 childBuild.setFinalName( parentBuild.getFinalName() );
330             }
331
332             ModelUtils.mergeFilterLists( childBuild.getFilters(), parentBuild.getFilters() );
333
334             List JavaDoc resources = childBuild.getResources();
335             if ( resources == null || resources.isEmpty() )
336             {
337                 childBuild.setResources( parentBuild.getResources() );
338             }
339
340             resources = childBuild.getTestResources();
341             if ( resources == null || resources.isEmpty() )
342             {
343                 childBuild.setTestResources( parentBuild.getTestResources() );
344             }
345
346             // Plugins are aggregated if Plugin.inherit != false
347
ModelUtils.mergePluginLists( childBuild, parentBuild, true );
348
349             // Plugin management :: aggregate
350
PluginManagement dominantPM = childBuild.getPluginManagement();
351             PluginManagement recessivePM = parentBuild.getPluginManagement();
352
353             if ( dominantPM == null && recessivePM != null )
354             {
355                 childBuild.setPluginManagement( recessivePM );
356             }
357             else
358             {
359                 ModelUtils.mergePluginLists( childBuild.getPluginManagement(), parentBuild.getPluginManagement(),
360                                              false );
361             }
362         }
363     }
364
365     private void assembleScmInheritance( Model child, Model parent, String JavaDoc childPathAdjustment, boolean appendPaths )
366     {
367         if ( parent.getScm() != null )
368         {
369             Scm parentScm = parent.getScm();
370
371             Scm childScm = child.getScm();
372
373             if ( childScm == null )
374             {
375                 childScm = new Scm();
376
377                 child.setScm( childScm );
378             }
379
380             if ( StringUtils.isEmpty( childScm.getConnection() ) && !StringUtils.isEmpty( parentScm.getConnection() ) )
381             {
382                 childScm.setConnection(
383                     appendPath( parentScm.getConnection(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
384             }
385
386             if ( StringUtils.isEmpty( childScm.getDeveloperConnection() ) &&
387                 !StringUtils.isEmpty( parentScm.getDeveloperConnection() ) )
388             {
389                 childScm
390                     .setDeveloperConnection( appendPath( parentScm.getDeveloperConnection(), child.getArtifactId(),
391                                                          childPathAdjustment, appendPaths ) );
392             }
393
394             if ( StringUtils.isEmpty( childScm.getUrl() ) && !StringUtils.isEmpty( parentScm.getUrl() ) )
395             {
396                 childScm.setUrl(
397                     appendPath( parentScm.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
398             }
399         }
400     }
401
402     private void assembleDistributionInheritence( Model child, Model parent, boolean appendPaths )
403     {
404         if ( parent.getDistributionManagement() != null )
405         {
406             DistributionManagement parentDistMgmt = parent.getDistributionManagement();
407
408             DistributionManagement childDistMgmt = child.getDistributionManagement();
409
410             if ( childDistMgmt == null )
411             {
412                 childDistMgmt = new DistributionManagement();
413
414                 child.setDistributionManagement( childDistMgmt );
415             }
416
417             if ( childDistMgmt.getSite() == null )
418             {
419                 if ( parentDistMgmt.getSite() != null )
420                 {
421                     Site site = new Site();
422
423                     childDistMgmt.setSite( site );
424
425                     site.setId( parentDistMgmt.getSite().getId() );
426
427                     site.setName( parentDistMgmt.getSite().getName() );
428
429                     site.setUrl( parentDistMgmt.getSite().getUrl() );
430
431                     if ( site.getUrl() != null )
432                     {
433                         site.setUrl(
434                             appendPath( site.getUrl(), child.getArtifactId(), null, appendPaths ) );
435                     }
436                 }
437             }
438
439             if ( childDistMgmt.getRepository() == null )
440             {
441                 if ( parentDistMgmt.getRepository() != null )
442                 {
443                     DeploymentRepository repository = copyDistributionRepository( parentDistMgmt.getRepository() );
444                     childDistMgmt.setRepository( repository );
445                 }
446             }
447
448             if ( childDistMgmt.getSnapshotRepository() == null )
449             {
450                 if ( parentDistMgmt.getSnapshotRepository() != null )
451                 {
452                     DeploymentRepository repository =
453                         copyDistributionRepository( parentDistMgmt.getSnapshotRepository() );
454                     childDistMgmt.setSnapshotRepository( repository );
455                 }
456             }
457
458             if ( StringUtils.isEmpty( childDistMgmt.getDownloadUrl() ) )
459             {
460                 childDistMgmt.setDownloadUrl( parentDistMgmt.getDownloadUrl() );
461             }
462
463             // NOTE: We SHOULD NOT be inheriting status, since this is an assessment of the POM quality.
464
// NOTE: We SHOULD NOT be inheriting relocation, since this relates to a single POM
465
}
466     }
467
468     private static DeploymentRepository copyDistributionRepository( DeploymentRepository parentRepository )
469     {
470         DeploymentRepository repository = new DeploymentRepository();
471
472         repository.setId( parentRepository.getId() );
473
474         repository.setName( parentRepository.getName() );
475
476         repository.setUrl( parentRepository.getUrl() );
477
478         repository.setLayout( parentRepository.getLayout() );
479
480         repository.setUniqueVersion( parentRepository.isUniqueVersion() );
481
482         return repository;
483     }
484
485     // TODO: This should eventually be migrated to DefaultPathTranslator.
486
protected String JavaDoc appendPath( String JavaDoc parentPath, String JavaDoc childPath, String JavaDoc pathAdjustment, boolean appendPaths )
487     {
488         String JavaDoc uncleanPath = parentPath;
489
490         if ( appendPaths )
491         {
492             if ( pathAdjustment != null )
493                 uncleanPath += "/" + pathAdjustment;
494
495             if ( childPath != null )
496                 uncleanPath += "/" + childPath;
497         }
498
499         String JavaDoc cleanedPath = "";
500
501         int protocolIdx = uncleanPath.indexOf( "://" );
502
503         if ( protocolIdx > -1 )
504         {
505             cleanedPath = uncleanPath.substring( 0, protocolIdx + 3 );
506             uncleanPath = uncleanPath.substring( protocolIdx + 3 );
507         }
508
509         if ( uncleanPath.startsWith( "/" ) )
510             cleanedPath += "/";
511
512         return cleanedPath + resolvePath( uncleanPath );
513     }
514
515     // TODO: Move this to plexus-utils' PathTool.
516
private static String JavaDoc resolvePath( String JavaDoc uncleanPath )
517     {
518         LinkedList JavaDoc pathElements = new LinkedList JavaDoc();
519
520         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( uncleanPath, "/" );
521
522         while ( tokenizer.hasMoreTokens() )
523         {
524             String JavaDoc token = (String JavaDoc) tokenizer.nextToken();
525
526             if ( token.equals( "" ) )
527             {
528                 // Empty path entry ("...//.."), remove.
529
}
530             else if ( token.equals( ".." ) )
531             {
532                 if ( pathElements.isEmpty() )
533                 {
534                     // FIXME: somehow report to the user
535
// that there are too many '..' elements.
536
// For now, ignore the extra '..'.
537
}
538                 else
539                 {
540                     pathElements.removeLast();
541                 }
542             }
543             else
544             {
545                 pathElements.addLast( token );
546             }
547         }
548
549
550         StringBuffer JavaDoc cleanedPath = new StringBuffer JavaDoc();
551
552         while ( !pathElements.isEmpty() )
553         {
554             cleanedPath.append( pathElements.removeFirst() );
555             if ( !pathElements.isEmpty() )
556                 cleanedPath.append( '/' );
557         }
558
559         return cleanedPath.toString();
560     }
561
562 }
563
Popular Tags