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