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