Coverage Report - org.apache.maven.project.injection.DefaultModelDefaultsInjector
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultModelDefaultsInjector
94 %
45/48
75 %
33/44
4,2
 
 1  
 package org.apache.maven.project.injection;
 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.Model;
 26  
 import org.apache.maven.model.Plugin;
 27  
 import org.apache.maven.model.PluginManagement;
 28  
 import org.apache.maven.project.ModelUtils;
 29  
 
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.TreeMap;
 34  
 
 35  
 /**
 36  
  * @author jdcasey Created on Feb 1, 2005
 37  
  */
 38  72
 public class DefaultModelDefaultsInjector
 39  
     implements ModelDefaultsInjector
 40  
 {
 41  
     public void injectDefaults( Model model )
 42  
     {
 43  122
         injectDependencyDefaults( model.getDependencies(), model.getDependencyManagement() );
 44  122
         if ( model.getBuild() != null )
 45  
         {
 46  111
             injectPluginDefaults( model.getBuild(), model.getBuild().getPluginManagement() );
 47  
         }
 48  122
     }
 49  
 
 50  
     private void injectPluginDefaults( Build build, PluginManagement pluginManagement )
 51  
     {
 52  111
         if ( pluginManagement == null )
 53  
         {
 54  
             // nothing to inject.
 55  0
             return ;
 56  
         }
 57  
         
 58  111
         List buildPlugins = build.getPlugins();
 59  
         
 60  111
         if ( buildPlugins != null && !buildPlugins.isEmpty() )
 61  
         {
 62  29
             Map pmPlugins = pluginManagement.getPluginsAsMap();
 63  
             
 64  29
             if ( pmPlugins != null && !pmPlugins.isEmpty() )
 65  
             {
 66  29
                 for ( Iterator it = buildPlugins.iterator(); it.hasNext(); )
 67  
                 {
 68  30
                     Plugin buildPlugin = (Plugin) it.next();
 69  
                     
 70  30
                     Plugin pmPlugin = (Plugin) pmPlugins.get( buildPlugin.getKey() );
 71  
                     
 72  30
                     if ( pmPlugin != null )
 73  
                     {
 74  24
                         mergePluginWithDefaults( buildPlugin, pmPlugin );
 75  
                     }
 76  30
                 }
 77  
             }
 78  
         }
 79  
         
 80  111
     }
 81  
 
 82  
     public void mergePluginWithDefaults( Plugin plugin, Plugin def )
 83  
     {
 84  24
         ModelUtils.mergePluginDefinitions( plugin, def, false );
 85  24
     }
 86  
 
 87  
     private void injectDependencyDefaults( List dependencies, DependencyManagement dependencyManagement )
 88  
     {
 89  122
         if ( dependencyManagement != null )
 90  
         {
 91  
             // a given project's dependencies should be smaller than the
 92  
             // group-defined defaults set...
 93  
             // in other words, the project's deps will probably be a subset of
 94  
             // those specified in defaults.
 95  35
             Map depsMap = new TreeMap();
 96  35
             for ( Iterator it = dependencies.iterator(); it.hasNext(); )
 97  
             {
 98  42
                 Dependency dep = (Dependency) it.next();
 99  42
                 depsMap.put( dep.getManagementKey(), dep );
 100  42
             }
 101  
 
 102  35
             List managedDependencies = dependencyManagement.getDependencies();
 103  
 
 104  35
             for ( Iterator it = managedDependencies.iterator(); it.hasNext(); )
 105  
             {
 106  79
                 Dependency def = (Dependency) it.next();
 107  79
                 String key = def.getManagementKey();
 108  
 
 109  79
                 Dependency dep = (Dependency) depsMap.get( key );
 110  79
                 if ( dep != null )
 111  
                 {
 112  39
                     mergeDependencyWithDefaults( dep, def );
 113  
                 }
 114  79
             }
 115  
         }
 116  122
     }
 117  
 
 118  
     private void mergeDependencyWithDefaults( Dependency dep, Dependency def )
 119  
     {
 120  39
         if ( dep.getScope() == null && def.getScope() != null )
 121  
         {
 122  5
             dep.setScope( def.getScope() );
 123  5
             dep.setSystemPath( def.getSystemPath() );
 124  
         }
 125  
 
 126  39
         if ( dep.getVersion() == null && def.getVersion() != null )
 127  
         {
 128  22
             dep.setVersion( def.getVersion() );
 129  
         }
 130  
         
 131  39
         if ( dep.getClassifier() == null && def.getClassifier() != null )
 132  
         {
 133  0
             dep.setClassifier( def.getClassifier() );
 134  
         }
 135  
         
 136  39
         if ( dep.getType() == null && def.getType() != null )
 137  
         {
 138  0
             dep.setType( def.getType() );
 139  
         }
 140  
         
 141  39
         List exclusions = dep.getExclusions();
 142  39
         if ( exclusions == null || exclusions.isEmpty() )
 143  
         {
 144  39
             dep.setExclusions( def.getExclusions() );
 145  
         }
 146  39
     }
 147  
 
 148  
 }