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