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      public void injectManagement( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
52      {
53          merger.mergeManagedBuildPlugins( model );
54      }
55  
56      protected static class ManagementModelMerger
57          extends MavenModelMerger
58      {
59  
60          public void mergeManagedBuildPlugins( Model model )
61          {
62              Build build = model.getBuild();
63              if ( build != null )
64              {
65                  PluginManagement pluginManagement = build.getPluginManagement();
66                  if ( pluginManagement != null )
67                  {
68                      mergePluginContainer_Plugins( build, pluginManagement );
69                  }
70              }
71          }
72  
73          private void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source )
74          {
75              List<Plugin> src = source.getPlugins();
76              if ( !src.isEmpty() )
77              {
78                  List<Plugin> tgt = target.getPlugins();
79  
80                  Map<Object, Plugin> managedPlugins = new LinkedHashMap<Object, Plugin>( src.size() * 2 );
81  
82                  Map<Object, Object> context = Collections.emptyMap();
83  
84                  for ( Plugin element : src )
85                  {
86                      Object key = getPluginKey( element );
87                      managedPlugins.put( key, element );
88                  }
89  
90                  for ( Plugin element : tgt )
91                  {
92                      Object key = getPluginKey( element );
93                      Plugin managedPlugin = managedPlugins.get( key );
94                      if ( managedPlugin != null )
95                      {
96                          mergePlugin( element, managedPlugin, false, context );
97                      }
98                  }
99              }
100         }
101 
102         @Override
103         protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sourceDominant,
104                                                Map<Object, Object> context )
105         {
106             List<PluginExecution> src = source.getExecutions();
107             if ( !src.isEmpty() )
108             {
109                 List<PluginExecution> tgt = target.getExecutions();
110 
111                 Map<Object, PluginExecution> merged =
112                     new LinkedHashMap<Object, PluginExecution>( ( src.size() + tgt.size() ) * 2 );
113 
114                 for ( PluginExecution element : src )
115                 {
116                     Object key = getPluginExecutionKey( element );
117                     merged.put( key, element.clone() );
118                 }
119 
120                 for ( PluginExecution element : tgt )
121                 {
122                     Object key = getPluginExecutionKey( element );
123                     PluginExecution existing = merged.get( key );
124                     if ( existing != null )
125                     {
126                         mergePluginExecution( element, existing, sourceDominant, context );
127                     }
128                     merged.put( key, element );
129                 }
130 
131                 target.setExecutions( new ArrayList<PluginExecution>( merged.values() ) );
132             }
133         }
134     }
135 
136 }