View Javadoc
1   package org.apache.maven.model.management;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.model.Build;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.model.PluginContainer;
32  import org.apache.maven.model.PluginExecution;
33  import org.apache.maven.model.PluginManagement;
34  import org.apache.maven.model.building.ModelBuildingRequest;
35  import org.apache.maven.model.building.ModelProblemCollector;
36  import org.apache.maven.model.merge.MavenModelMerger;
37  import org.codehaus.plexus.component.annotations.Component;
38  
39  /**
40   * Handles injection of plugin management into the model.
41   *
42   * @author Benjamin Bentmann
43   */
44  @Component( role = PluginManagementInjector.class )
45  public class DefaultPluginManagementInjector
46      implements PluginManagementInjector
47  {
48  
49      private ManagementModelMerger merger = new ManagementModelMerger();
50  
51      @Override
52      public void injectManagement( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
53      {
54          merger.mergeManagedBuildPlugins( model );
55      }
56  
57      protected static class ManagementModelMerger
58          extends MavenModelMerger
59      {
60  
61          public void mergeManagedBuildPlugins( Model model )
62          {
63              Build build = model.getBuild();
64              if ( build != null )
65              {
66                  PluginManagement pluginManagement = build.getPluginManagement();
67                  if ( pluginManagement != null )
68                  {
69                      mergePluginContainer_Plugins( build, pluginManagement );
70                  }
71              }
72          }
73  
74          private void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source )
75          {
76              List<Plugin> src = source.getPlugins();
77              if ( !src.isEmpty() )
78              {
79                  List<Plugin> tgt = target.getPlugins();
80  
81                  Map<Object, Plugin> managedPlugins = new LinkedHashMap<Object, Plugin>( src.size() * 2 );
82  
83                  Map<Object, Object> context = Collections.emptyMap();
84  
85                  for ( Plugin element : src )
86                  {
87                      Object key = getPluginKey( element );
88                      managedPlugins.put( key, element );
89                  }
90  
91                  for ( Plugin element : tgt )
92                  {
93                      Object key = getPluginKey( element );
94                      Plugin managedPlugin = managedPlugins.get( key );
95                      if ( managedPlugin != null )
96                      {
97                          mergePlugin( element, managedPlugin, false, context );
98                      }
99                  }
100             }
101         }
102 
103         @Override
104         protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sourceDominant,
105                                                Map<Object, Object> context )
106         {
107             List<PluginExecution> src = source.getExecutions();
108             if ( !src.isEmpty() )
109             {
110                 List<PluginExecution> tgt = target.getExecutions();
111 
112                 Map<Object, PluginExecution> merged =
113                     new LinkedHashMap<Object, PluginExecution>( ( src.size() + tgt.size() ) * 2 );
114 
115                 for ( PluginExecution element : src )
116                 {
117                     Object key = getPluginExecutionKey( element );
118                     merged.put( key, element.clone() );
119                 }
120 
121                 for ( PluginExecution element : tgt )
122                 {
123                     Object key = getPluginExecutionKey( element );
124                     PluginExecution existing = merged.get( key );
125                     if ( existing != null )
126                     {
127                         mergePluginExecution( element, existing, sourceDominant, context );
128                     }
129                     merged.put( key, element );
130                 }
131 
132                 target.setExecutions( new ArrayList<PluginExecution>( merged.values() ) );
133             }
134         }
135     }
136 
137 }