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