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