1 package org.apache.maven.model.management;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
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
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 }