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