View Javadoc

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  public class DefaultModelDefaultsInjector
39      implements ModelDefaultsInjector
40  {
41      public void injectDefaults( Model model )
42      {
43          injectDependencyDefaults( model.getDependencies(), model.getDependencyManagement() );
44          if ( model.getBuild() != null )
45          {
46              injectPluginDefaults( model.getBuild(), model.getBuild().getPluginManagement() );
47          }
48      }
49  
50      private void injectPluginDefaults( Build build, PluginManagement pluginManagement )
51      {
52          if ( pluginManagement == null )
53          {
54              // nothing to inject.
55              return ;
56          }
57          
58          List buildPlugins = build.getPlugins();
59          
60          if ( buildPlugins != null && !buildPlugins.isEmpty() )
61          {
62              Map pmPlugins = pluginManagement.getPluginsAsMap();
63              
64              if ( pmPlugins != null && !pmPlugins.isEmpty() )
65              {
66                  for ( Iterator it = buildPlugins.iterator(); it.hasNext(); )
67                  {
68                      Plugin buildPlugin = (Plugin) it.next();
69                      
70                      Plugin pmPlugin = (Plugin) pmPlugins.get( buildPlugin.getKey() );
71                      
72                      if ( pmPlugin != null )
73                      {
74                          mergePluginWithDefaults( buildPlugin, pmPlugin );
75                      }
76                  }
77              }
78          }
79          
80      }
81  
82      public void mergePluginWithDefaults( Plugin plugin, Plugin def )
83      {
84          ModelUtils.mergePluginDefinitions( plugin, def, false );
85      }
86  
87      private void injectDependencyDefaults( List dependencies, DependencyManagement dependencyManagement )
88      {
89          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              Map depsMap = new TreeMap();
96              for ( Iterator it = dependencies.iterator(); it.hasNext(); )
97              {
98                  Dependency dep = (Dependency) it.next();
99                  depsMap.put( dep.getManagementKey(), dep );
100             }
101 
102             List managedDependencies = dependencyManagement.getDependencies();
103 
104             for ( Iterator it = managedDependencies.iterator(); it.hasNext(); )
105             {
106                 Dependency def = (Dependency) it.next();
107                 String key = def.getManagementKey();
108 
109                 Dependency dep = (Dependency) depsMap.get( key );
110                 if ( dep != null )
111                 {
112                     mergeDependencyWithDefaults( dep, def );
113                 }
114             }
115         }
116     }
117 
118     private void mergeDependencyWithDefaults( Dependency dep, Dependency def )
119     {
120         if ( dep.getScope() == null && def.getScope() != null )
121         {
122             dep.setScope( def.getScope() );
123             dep.setSystemPath( def.getSystemPath() );
124         }
125 
126         if ( dep.getVersion() == null && def.getVersion() != null )
127         {
128             dep.setVersion( def.getVersion() );
129         }
130         
131         if ( dep.getClassifier() == null && def.getClassifier() != null )
132         {
133             dep.setClassifier( def.getClassifier() );
134         }
135         
136         if ( dep.getType() == null && def.getType() != null )
137         {
138             dep.setType( def.getType() );
139         }
140         
141         List exclusions = dep.getExclusions();
142         if ( exclusions == null || exclusions.isEmpty() )
143         {
144             dep.setExclusions( def.getExclusions() );
145         }
146     }
147 
148 }