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