Coverage Report - org.apache.maven.project.inheritance.DefaultModelInheritanceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultModelInheritanceAssembler
0%
0/195
0%
0/140
6
 
 1  
 package org.apache.maven.project.inheritance;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.model.Build;
 23  
 import org.apache.maven.model.Dependency;
 24  
 import org.apache.maven.model.DependencyManagement;
 25  
 import org.apache.maven.model.DeploymentRepository;
 26  
 import org.apache.maven.model.DistributionManagement;
 27  
 import org.apache.maven.model.Model;
 28  
 import org.apache.maven.model.PluginManagement;
 29  
 import org.apache.maven.model.Reporting;
 30  
 import org.apache.maven.model.Scm;
 31  
 import org.apache.maven.model.Site;
 32  
 import org.apache.maven.project.ModelUtils;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 
 35  
 import java.util.ArrayList;
 36  
 import java.util.Iterator;
 37  
 import java.util.LinkedList;
 38  
 import java.util.List;
 39  
 import java.util.Map;
 40  
 import java.util.Properties;
 41  
 import java.util.StringTokenizer;
 42  
 import java.util.TreeMap;
 43  
 
 44  
 /**
 45  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
 46  
  * @version $Id: DefaultModelInheritanceAssembler.java,v 1.4 2004/08/23 20:24:54
 47  
  *          jdcasey Exp $
 48  
  * @todo generate this with modello to keep it in sync with changes in the model.
 49  
  */
 50  0
 public class DefaultModelInheritanceAssembler
 51  
     implements ModelInheritanceAssembler
 52  
 {
 53  
     public void copyModel( Model dest, Model source )
 54  
     {
 55  0
         assembleModelInheritance( dest, source, null, false );
 56  0
     }
 57  
 
 58  
     public void assembleModelInheritance( Model child, Model parent, String childPathAdjustment )
 59  
     {
 60  0
         assembleModelInheritance( child, parent, childPathAdjustment, true );
 61  0
     }
 62  
 
 63  
     public void assembleModelInheritance( Model child, Model parent )
 64  
     {
 65  0
         assembleModelInheritance( child, parent, null, true );
 66  0
     }
 67  
 
 68  
     private void assembleModelInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
 69  
     {
 70  
         // cannot inherit from null parent.
 71  0
         if ( parent == null )
 72  
         {
 73  0
             return;
 74  
         }
 75  
 
 76  
         // Group id
 77  0
         if ( child.getGroupId() == null )
 78  
         {
 79  0
             child.setGroupId( parent.getGroupId() );
 80  
         }
 81  
 
 82  
         // version
 83  0
         if ( child.getVersion() == null )
 84  
         {
 85  
             // The parent version may have resolved to something different, so we take what we asked for...
 86  
             // instead of - child.setVersion( parent.getVersion() );
 87  
 
 88  0
             if ( child.getParent() != null )
 89  
             {
 90  0
                 child.setVersion( child.getParent().getVersion() );
 91  
             }
 92  
         }
 93  
 
 94  
         // inceptionYear
 95  0
         if ( child.getInceptionYear() == null )
 96  
         {
 97  0
             child.setInceptionYear( parent.getInceptionYear() );
 98  
         }
 99  
 
 100  
         // url
 101  0
         if ( child.getUrl() == null )
 102  
         {
 103  0
             if ( parent.getUrl() != null )
 104  
             {
 105  0
                 child.setUrl( appendPath( parent.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
 106  
             }
 107  
             else
 108  
             {
 109  0
                 child.setUrl( parent.getUrl() );
 110  
             }
 111  
         }
 112  
 
 113  0
         assembleDistributionInheritence( child, parent, childPathAdjustment, appendPaths );
 114  
 
 115  
         // issueManagement
 116  0
         if ( child.getIssueManagement() == null )
 117  
         {
 118  0
             child.setIssueManagement( parent.getIssueManagement() );
 119  
         }
 120  
 
 121  
         // description
 122  0
         if ( child.getDescription() == null )
 123  
         {
 124  0
             child.setDescription( parent.getDescription() );
 125  
         }
 126  
 
 127  
         // Organization
 128  0
         if ( child.getOrganization() == null )
 129  
         {
 130  0
             child.setOrganization( parent.getOrganization() );
 131  
         }
 132  
 
 133  
         // Scm
 134  0
         assembleScmInheritance( child, parent, childPathAdjustment, appendPaths );
 135  
 
 136  
         // ciManagement
 137  0
         if ( child.getCiManagement() == null )
 138  
         {
 139  0
             child.setCiManagement( parent.getCiManagement() );
 140  
         }
 141  
 
 142  
         // developers
 143  0
         if ( child.getDevelopers().size() == 0 )
 144  
         {
 145  0
             child.setDevelopers( parent.getDevelopers() );
 146  
         }
 147  
 
 148  
         // licenses
 149  0
         if ( child.getLicenses().size() == 0 )
 150  
         {
 151  0
             child.setLicenses( parent.getLicenses() );
 152  
         }
 153  
 
 154  
         // developers
 155  0
         if ( child.getContributors().size() == 0 )
 156  
         {
 157  0
             child.setContributors( parent.getContributors() );
 158  
         }
 159  
 
 160  
         // mailingLists
 161  0
         if ( child.getMailingLists().size() == 0 )
 162  
         {
 163  0
             child.setMailingLists( parent.getMailingLists() );
 164  
         }
 165  
 
 166  
         // Build
 167  0
         assembleBuildInheritance( child, parent );
 168  
 
 169  0
         assembleDependencyInheritance( child, parent );
 170  
 
 171  0
         child.setRepositories( ModelUtils.mergeRepositoryLists( child.getRepositories(), parent.getRepositories() ) );
 172  0
         child.setPluginRepositories(
 173  
             ModelUtils.mergeRepositoryLists( child.getPluginRepositories(), parent.getPluginRepositories() ) );
 174  
 
 175  0
         assembleReportingInheritance( child, parent );
 176  
 
 177  0
         assembleDependencyManagementInheritance( child, parent );
 178  
 
 179  0
         Properties props = new Properties();
 180  0
         props.putAll( parent.getProperties() );
 181  0
         props.putAll( child.getProperties() );
 182  
 
 183  0
         child.setProperties( props );
 184  0
     }
 185  
 
 186  
     private void assembleDependencyManagementInheritance( Model child, Model parent )
 187  
     {
 188  0
         DependencyManagement parentDepMgmt = parent.getDependencyManagement();
 189  
 
 190  0
         DependencyManagement childDepMgmt = child.getDependencyManagement();
 191  
 
 192  0
         if ( parentDepMgmt != null )
 193  
         {
 194  0
             if ( childDepMgmt == null )
 195  
             {
 196  0
                 child.setDependencyManagement( parentDepMgmt );
 197  
             }
 198  
             else
 199  
             {
 200  0
                 List childDeps = childDepMgmt.getDependencies();
 201  
 
 202  0
                 Map mappedChildDeps = new TreeMap();
 203  0
                 for ( Iterator it = childDeps.iterator(); it.hasNext(); )
 204  
                 {
 205  0
                     Dependency dep = (Dependency) it.next();
 206  0
                     mappedChildDeps.put( dep.getManagementKey(), dep );
 207  0
                 }
 208  
 
 209  0
                 for ( Iterator it = parentDepMgmt.getDependencies().iterator(); it.hasNext(); )
 210  
                 {
 211  0
                     Dependency dep = (Dependency) it.next();
 212  0
                     if ( !mappedChildDeps.containsKey( dep.getManagementKey() ) )
 213  
                     {
 214  0
                         childDepMgmt.addDependency( dep );
 215  
                     }
 216  0
                 }
 217  
             }
 218  
         }
 219  0
     }
 220  
 
 221  
     private void assembleReportingInheritance( Model child, Model parent )
 222  
     {
 223  
         // Reports :: aggregate
 224  0
         Reporting childReporting = child.getReporting();
 225  0
         Reporting parentReporting = parent.getReporting();
 226  
 
 227  0
         if ( parentReporting != null )
 228  
         {
 229  0
             if ( childReporting == null )
 230  
             {
 231  0
                 childReporting = new Reporting();
 232  0
                 child.setReporting( childReporting );
 233  
             }
 234  
 
 235  0
             if ( childReporting.isExcludeDefaultsValue() == null )
 236  
             {
 237  0
                 childReporting.setExcludeDefaultsValue( parentReporting.isExcludeDefaultsValue() );
 238  
             }
 239  
 
 240  0
             if ( StringUtils.isEmpty( childReporting.getOutputDirectory() ) )
 241  
             {
 242  0
                 childReporting.setOutputDirectory( parentReporting.getOutputDirectory() );
 243  
             }
 244  
 
 245  0
             ModelUtils.mergeReportPluginLists( childReporting, parentReporting, true );
 246  
         }
 247  0
     }
 248  
 
 249  
     private void assembleDependencyInheritance( Model child, Model parent )
 250  
     {
 251  0
         child.setDependencies( ModelUtils.mergeDependencyList( child.getDependencies(), parent.getDependencies() ) );
 252  0
     }
 253  
 
 254  
     private void assembleBuildInheritance( Model child, Model parent )
 255  
     {
 256  0
         Build childBuild = child.getBuild();
 257  0
         Build parentBuild = parent.getBuild();
 258  
 
 259  0
         if ( parentBuild != null )
 260  
         {
 261  0
             if ( childBuild == null )
 262  
             {
 263  0
                 childBuild = new Build();
 264  0
                 child.setBuild( childBuild );
 265  
             }
 266  
 
 267  
             // The build has been set but we want to step in here and fill in
 268  
             // values that have not been set by the child.
 269  
 
 270  0
             if ( childBuild.getSourceDirectory() == null )
 271  
             {
 272  0
                 childBuild.setSourceDirectory( parentBuild.getSourceDirectory() );
 273  
             }
 274  
 
 275  0
             if ( childBuild.getScriptSourceDirectory() == null )
 276  
             {
 277  0
                 childBuild.setScriptSourceDirectory( parentBuild.getScriptSourceDirectory() );
 278  
             }
 279  
 
 280  0
             if ( childBuild.getTestSourceDirectory() == null )
 281  
             {
 282  0
                 childBuild.setTestSourceDirectory( parentBuild.getTestSourceDirectory() );
 283  
             }
 284  
 
 285  0
             if ( childBuild.getOutputDirectory() == null )
 286  
             {
 287  0
                 childBuild.setOutputDirectory( parentBuild.getOutputDirectory() );
 288  
             }
 289  
 
 290  0
             if ( childBuild.getTestOutputDirectory() == null )
 291  
             {
 292  0
                 childBuild.setTestOutputDirectory( parentBuild.getTestOutputDirectory() );
 293  
             }
 294  
 
 295  
             // Extensions are accumlated
 296  0
             ModelUtils.mergeExtensionLists( childBuild, parentBuild );
 297  
 
 298  0
             if ( childBuild.getDirectory() == null )
 299  
             {
 300  0
                 childBuild.setDirectory( parentBuild.getDirectory() );
 301  
             }
 302  
 
 303  0
             if ( childBuild.getDefaultGoal() == null )
 304  
             {
 305  0
                 childBuild.setDefaultGoal( parentBuild.getDefaultGoal() );
 306  
             }
 307  
 
 308  0
             if ( childBuild.getFinalName() == null )
 309  
             {
 310  0
                 childBuild.setFinalName( parentBuild.getFinalName() );
 311  
             }
 312  
 
 313  0
             ModelUtils.mergeFilterLists( childBuild.getFilters(), parentBuild.getFilters() );
 314  
 
 315  0
             List resources = childBuild.getResources();
 316  0
             if ( ( resources == null ) || resources.isEmpty() )
 317  
             {
 318  0
                 childBuild.setResources( parentBuild.getResources() );
 319  
             }
 320  
 
 321  0
             resources = childBuild.getTestResources();
 322  0
             if ( ( resources == null ) || resources.isEmpty() )
 323  
             {
 324  0
                 childBuild.setTestResources( parentBuild.getTestResources() );
 325  
             }
 326  
 
 327  
             // Plugins are aggregated if Plugin.inherit != false
 328  0
             ModelUtils.mergePluginLists( childBuild, parentBuild, true );
 329  
 
 330  
             // Plugin management :: aggregate
 331  0
             PluginManagement dominantPM = childBuild.getPluginManagement();
 332  0
             PluginManagement recessivePM = parentBuild.getPluginManagement();
 333  
 
 334  0
             if ( ( dominantPM == null ) && ( recessivePM != null ) )
 335  
             {
 336  
                 // FIXME: Filter out the inherited == false stuff!
 337  0
                 childBuild.setPluginManagement( recessivePM );
 338  
             }
 339  
             else
 340  
             {
 341  0
                 ModelUtils.mergePluginLists( childBuild.getPluginManagement(), parentBuild.getPluginManagement(),
 342  
                                              false );
 343  
             }
 344  
         }
 345  0
     }
 346  
 
 347  
     private void assembleScmInheritance( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
 348  
     {
 349  0
         if ( parent.getScm() != null )
 350  
         {
 351  0
             Scm parentScm = parent.getScm();
 352  
 
 353  0
             Scm childScm = child.getScm();
 354  
 
 355  0
             if ( childScm == null )
 356  
             {
 357  0
                 childScm = new Scm();
 358  
 
 359  0
                 child.setScm( childScm );
 360  
             }
 361  
 
 362  0
             if ( StringUtils.isEmpty( childScm.getConnection() ) && !StringUtils.isEmpty( parentScm.getConnection() ) )
 363  
             {
 364  0
                 childScm.setConnection(
 365  
                     appendPath( parentScm.getConnection(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
 366  
             }
 367  
 
 368  0
             if ( StringUtils.isEmpty( childScm.getDeveloperConnection() ) &&
 369  
                 !StringUtils.isEmpty( parentScm.getDeveloperConnection() ) )
 370  
             {
 371  0
                 childScm
 372  
                     .setDeveloperConnection( appendPath( parentScm.getDeveloperConnection(), child.getArtifactId(),
 373  
                                                          childPathAdjustment, appendPaths ) );
 374  
             }
 375  
 
 376  0
             if ( StringUtils.isEmpty( childScm.getUrl() ) && !StringUtils.isEmpty( parentScm.getUrl() ) )
 377  
             {
 378  0
                 childScm.setUrl(
 379  
                     appendPath( parentScm.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
 380  
             }
 381  
         }
 382  0
     }
 383  
 
 384  
     private void assembleDistributionInheritence( Model child, Model parent, String childPathAdjustment, boolean appendPaths )
 385  
     {
 386  0
         if ( parent.getDistributionManagement() != null )
 387  
         {
 388  0
             DistributionManagement parentDistMgmt = parent.getDistributionManagement();
 389  
 
 390  0
             DistributionManagement childDistMgmt = child.getDistributionManagement();
 391  
 
 392  0
             if ( childDistMgmt == null )
 393  
             {
 394  0
                 childDistMgmt = new DistributionManagement();
 395  
 
 396  0
                 child.setDistributionManagement( childDistMgmt );
 397  
             }
 398  
 
 399  0
             if ( childDistMgmt.getSite() == null )
 400  
             {
 401  0
                 if ( parentDistMgmt.getSite() != null )
 402  
                 {
 403  0
                     Site site = new Site();
 404  
 
 405  0
                     childDistMgmt.setSite( site );
 406  
 
 407  0
                     site.setId( parentDistMgmt.getSite().getId() );
 408  
 
 409  0
                     site.setName( parentDistMgmt.getSite().getName() );
 410  
 
 411  0
                     site.setUrl( parentDistMgmt.getSite().getUrl() );
 412  
 
 413  0
                     if ( site.getUrl() != null )
 414  
                     {
 415  0
                         site.setUrl(
 416  
                             appendPath( site.getUrl(), child.getArtifactId(), childPathAdjustment, appendPaths ) );
 417  
                     }
 418  
                 }
 419  
             }
 420  
 
 421  0
             if ( childDistMgmt.getRepository() == null )
 422  
             {
 423  0
                 if ( parentDistMgmt.getRepository() != null )
 424  
                 {
 425  0
                     DeploymentRepository repository = copyDistributionRepository( parentDistMgmt.getRepository() );
 426  0
                     childDistMgmt.setRepository( repository );
 427  
                 }
 428  
             }
 429  
 
 430  0
             if ( childDistMgmt.getSnapshotRepository() == null )
 431  
             {
 432  0
                 if ( parentDistMgmt.getSnapshotRepository() != null )
 433  
                 {
 434  0
                     DeploymentRepository repository =
 435  
                         copyDistributionRepository( parentDistMgmt.getSnapshotRepository() );
 436  0
                     childDistMgmt.setSnapshotRepository( repository );
 437  
                 }
 438  
             }
 439  
 
 440  0
             if ( StringUtils.isEmpty( childDistMgmt.getDownloadUrl() ) )
 441  
             {
 442  0
                 childDistMgmt.setDownloadUrl( parentDistMgmt.getDownloadUrl() );
 443  
             }
 444  
 
 445  
             // NOTE: We SHOULD NOT be inheriting status, since this is an assessment of the POM quality.
 446  
             // NOTE: We SHOULD NOT be inheriting relocation, since this relates to a single POM
 447  
         }
 448  0
     }
 449  
 
 450  
     private static DeploymentRepository copyDistributionRepository( DeploymentRepository parentRepository )
 451  
     {
 452  0
         DeploymentRepository repository = new DeploymentRepository();
 453  
 
 454  0
         repository.setId( parentRepository.getId() );
 455  
 
 456  0
         repository.setName( parentRepository.getName() );
 457  
 
 458  0
         repository.setUrl( parentRepository.getUrl() );
 459  
 
 460  0
         repository.setLayout( parentRepository.getLayout() );
 461  
 
 462  0
         repository.setUniqueVersion( parentRepository.isUniqueVersion() );
 463  
 
 464  0
         return repository;
 465  
     }
 466  
 
 467  
     // TODO: This should eventually be migrated to DefaultPathTranslator.
 468  
     protected String appendPath( String parentPath, String childPath, String pathAdjustment, boolean appendPaths )
 469  
     {
 470  0
         String uncleanPath = parentPath;
 471  
 
 472  0
         if ( appendPaths )
 473  
         {
 474  0
             if ( pathAdjustment != null )
 475  
             {
 476  0
                 uncleanPath += "/" + pathAdjustment;
 477  
             }
 478  
 
 479  0
             if ( childPath != null )
 480  
             {
 481  0
                 uncleanPath += "/" + childPath;
 482  
             }
 483  
         }
 484  
 
 485  0
         String cleanedPath = "";
 486  
 
 487  0
         int protocolIdx = uncleanPath.indexOf( "://" );
 488  
 
 489  0
         if ( protocolIdx > -1 )
 490  
         {
 491  0
             cleanedPath = uncleanPath.substring( 0, protocolIdx + 3 );
 492  0
             uncleanPath = uncleanPath.substring( protocolIdx + 3 );
 493  
         }
 494  
 
 495  0
         if ( uncleanPath.startsWith( "//" ) )
 496  
         {
 497  
             // preserve leading double slash for UNC paths like "file:////host/pom.xml"
 498  0
             cleanedPath += "//";
 499  
         }
 500  0
         else if ( uncleanPath.startsWith( "/" ) )
 501  
         {
 502  0
             cleanedPath += "/";
 503  
         }
 504  
 
 505  0
         return cleanedPath + resolvePath( uncleanPath );
 506  
     }
 507  
 
 508  
     // TODO: Move this to plexus-utils' PathTool.
 509  
     private static String resolvePath( String uncleanPath )
 510  
     {
 511  0
         LinkedList pathElements = new LinkedList();
 512  
 
 513  0
         StringTokenizer tokenizer = new StringTokenizer( uncleanPath, "/" );
 514  
 
 515  0
         while ( tokenizer.hasMoreTokens() )
 516  
         {
 517  0
             String token = tokenizer.nextToken();
 518  
 
 519  0
             if ( token.equals( "" ) )
 520  
             {
 521  
                 // Empty path entry ("...//.."), remove.
 522  
             }
 523  0
             else if ( token.equals( ".." ) )
 524  
             {
 525  0
                 if ( pathElements.isEmpty() )
 526  
                 {
 527  
                     // FIXME: somehow report to the user
 528  
                     // that there are too many '..' elements.
 529  
                     // For now, ignore the extra '..'.
 530  
                 }
 531  
                 else
 532  
                 {
 533  0
                     pathElements.removeLast();
 534  
                 }
 535  
             }
 536  
             else
 537  
             {
 538  0
                 pathElements.addLast( token );
 539  
             }
 540  0
         }
 541  
 
 542  
 
 543  0
         StringBuffer cleanedPath = new StringBuffer();
 544  
 
 545  0
         while ( !pathElements.isEmpty() )
 546  
         {
 547  0
             cleanedPath.append( pathElements.removeFirst() );
 548  0
             if ( !pathElements.isEmpty() )
 549  
             {
 550  0
                 cleanedPath.append( '/' );
 551  
             }
 552  
         }
 553  
 
 554  0
         return cleanedPath.toString();
 555  
     }
 556  
 
 557  
 }