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