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 javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.apache.maven.api.model.Build;
32  import org.apache.maven.api.model.Model;
33  import org.apache.maven.api.model.Plugin;
34  import org.apache.maven.api.model.PluginContainer;
35  import org.apache.maven.api.model.PluginExecution;
36  import org.apache.maven.api.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( org.apache.maven.model.Model model, ModelBuildingRequest request,
57                                     ModelProblemCollector problems )
58      {
59          model.update( merger.mergeManagedBuildPlugins( model.getDelegate() ) );
60      }
61  
62      /**
63       * ManagementModelMerger
64       */
65      protected static class ManagementModelMerger
66          extends MavenModelMerger
67      {
68  
69          public Model mergeManagedBuildPlugins( Model model )
70          {
71              Build build = model.getBuild();
72              if ( build != null )
73              {
74                  PluginManagement pluginManagement = build.getPluginManagement();
75                  if ( pluginManagement != null )
76                  {
77                      return model.withBuild( mergePluginContainerPlugins( build, pluginManagement ) );
78                  }
79              }
80              return model;
81          }
82  
83          private Build mergePluginContainerPlugins( Build target, PluginContainer source )
84          {
85              List<Plugin> src = source.getPlugins();
86              if ( !src.isEmpty() )
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().apply( element );
95                      managedPlugins.put( key, element );
96                  }
97  
98                  List<Plugin> newPlugins = new ArrayList<>();
99                  for ( Plugin element : target.getPlugins() )
100                 {
101                     Object key = getPluginKey().apply( element );
102                     Plugin managedPlugin = managedPlugins.get( key );
103                     if ( managedPlugin != null )
104                     {
105                         element = mergePlugin( element, managedPlugin, false, context );
106                     }
107                     newPlugins.add( element );
108                 }
109                 return target.withPlugins( newPlugins );
110             }
111             return target;
112         }
113 
114         @Override
115         protected void mergePlugin_Executions( Plugin.Builder builder, Plugin target, Plugin source,
116                                                boolean sourceDominant, Map<Object, Object> context )
117         {
118             List<PluginExecution> src = source.getExecutions();
119             if ( !src.isEmpty() )
120             {
121                 List<PluginExecution> tgt = target.getExecutions();
122 
123                 Map<Object, PluginExecution> merged =
124                     new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
125 
126                 for ( PluginExecution element : src )
127                 {
128                     Object key = getPluginExecutionKey().apply( element );
129                     merged.put( key, element );
130                 }
131 
132                 for ( PluginExecution element : tgt )
133                 {
134                     Object key = getPluginExecutionKey().apply( element );
135                     PluginExecution existing = merged.get( key );
136                     if ( existing != null )
137                     {
138                         element = mergePluginExecution( element, existing, sourceDominant, context );
139                     }
140                     merged.put( key, element );
141                 }
142 
143                 builder.executions( merged.values() );
144             }
145         }
146     }
147 
148 }