View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.management;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.maven.model.Build;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.model.PluginContainer;
34  import org.apache.maven.model.PluginExecution;
35  import org.apache.maven.model.PluginManagement;
36  import org.apache.maven.model.building.ModelBuildingRequest;
37  import org.apache.maven.model.building.ModelProblemCollector;
38  import org.apache.maven.model.merge.MavenModelMerger;
39  
40  /**
41   * Handles injection of plugin management into the model.
42   *
43   * @author Benjamin Bentmann
44   */
45  @SuppressWarnings({"checkstyle:methodname"})
46  @Named
47  @Singleton
48  public class DefaultPluginManagementInjector implements PluginManagementInjector {
49  
50      private ManagementModelMerger merger = new ManagementModelMerger();
51  
52      @Override
53      public void injectManagement(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
54          merger.mergeManagedBuildPlugins(model);
55      }
56  
57      /**
58       * ManagementModelMerger
59       */
60      protected static class ManagementModelMerger extends MavenModelMerger {
61  
62          public void mergeManagedBuildPlugins(Model model) {
63              Build build = model.getBuild();
64              if (build != null) {
65                  PluginManagement pluginManagement = build.getPluginManagement();
66                  if (pluginManagement != null) {
67                      mergePluginContainerPlugins(build, pluginManagement);
68                  }
69              }
70          }
71  
72          private void mergePluginContainerPlugins(PluginContainer target, PluginContainer source) {
73              List<Plugin> src = source.getPlugins();
74              if (!src.isEmpty()) {
75                  List<Plugin> tgt = target.getPlugins();
76  
77                  Map<Object, Plugin> managedPlugins = new LinkedHashMap<>(src.size() * 2);
78  
79                  Map<Object, Object> context = Collections.emptyMap();
80  
81                  for (Plugin element : src) {
82                      Object key = getPluginKey(element);
83                      managedPlugins.put(key, element);
84                  }
85  
86                  for (Plugin element : tgt) {
87                      Object key = getPluginKey(element);
88                      Plugin managedPlugin = managedPlugins.get(key);
89                      if (managedPlugin != null) {
90                          mergePlugin(element, managedPlugin, false, context);
91                      }
92                  }
93              }
94          }
95  
96          @Override
97          protected void mergePlugin_Executions(
98                  Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context) {
99              List<PluginExecution> src = source.getExecutions();
100             if (!src.isEmpty()) {
101                 List<PluginExecution> tgt = target.getExecutions();
102 
103                 Map<Object, PluginExecution> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
104 
105                 for (PluginExecution element : src) {
106                     Object key = getPluginExecutionKey(element);
107                     merged.put(key, element.clone());
108                 }
109 
110                 for (PluginExecution element : tgt) {
111                     Object key = getPluginExecutionKey(element);
112                     PluginExecution existing = merged.get(key);
113                     if (existing != null) {
114                         mergePluginExecution(element, existing, sourceDominant, context);
115                     }
116                     merged.put(key, element);
117                 }
118 
119                 target.setExecutions(new ArrayList<>(merged.values()));
120             }
121         }
122     }
123 }