1 package org.apache.maven.model.profile;
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.BuildBase;
33 import org.apache.maven.model.Model;
34 import org.apache.maven.model.ModelBase;
35 import org.apache.maven.model.Plugin;
36 import org.apache.maven.model.PluginContainer;
37 import org.apache.maven.model.PluginExecution;
38 import org.apache.maven.model.Profile;
39 import org.apache.maven.model.ReportPlugin;
40 import org.apache.maven.model.ReportSet;
41 import org.apache.maven.model.Reporting;
42 import org.apache.maven.model.building.ModelBuildingRequest;
43 import org.apache.maven.model.building.ModelProblemCollector;
44 import org.apache.maven.model.merge.MavenModelMerger;
45
46
47
48
49
50
51 @Named
52 @Singleton
53 @SuppressWarnings( { "checkstyle:methodname" } )
54 public class DefaultProfileInjector
55 implements ProfileInjector
56 {
57
58 private ProfileModelMerger merger = new ProfileModelMerger();
59
60 @Override
61 public void injectProfile( Model model, Profile profile, ModelBuildingRequest request,
62 ModelProblemCollector problems )
63 {
64 if ( profile != null )
65 {
66 merger.mergeModelBase( model, profile );
67
68 if ( profile.getBuild() != null )
69 {
70 if ( model.getBuild() == null )
71 {
72 model.setBuild( new Build() );
73 }
74 merger.mergeBuildBase( model.getBuild(), profile.getBuild() );
75 }
76 }
77 }
78
79
80
81
82 protected static class ProfileModelMerger
83 extends MavenModelMerger
84 {
85
86 public void mergeModelBase( ModelBase target, ModelBase source )
87 {
88 mergeModelBase( target, source, true, Collections.emptyMap() );
89 }
90
91 public void mergeBuildBase( BuildBase target, BuildBase source )
92 {
93 mergeBuildBase( target, source, true, Collections.emptyMap() );
94 }
95
96 @Override
97 protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
98 boolean sourceDominant, Map<Object, Object> context )
99 {
100 List<Plugin> src = source.getPlugins();
101 if ( !src.isEmpty() )
102 {
103 List<Plugin> tgt = target.getPlugins();
104 Map<Object, Plugin> master = new LinkedHashMap<>( tgt.size() * 2 );
105
106 for ( Plugin element : tgt )
107 {
108 Object key = getPluginKey( element );
109 master.put( key, element );
110 }
111
112 Map<Object, List<Plugin>> predecessors = new LinkedHashMap<>();
113 List<Plugin> pending = new ArrayList<>();
114 for ( Plugin element : src )
115 {
116 Object key = getPluginKey( element );
117 Plugin existing = master.get( key );
118 if ( existing != null )
119 {
120 mergePlugin( existing, element, sourceDominant, context );
121
122 if ( !pending.isEmpty() )
123 {
124 predecessors.put( key, pending );
125 pending = new ArrayList<>();
126 }
127 }
128 else
129 {
130 pending.add( element );
131 }
132 }
133
134 List<Plugin> result = new ArrayList<>( src.size() + tgt.size() );
135 for ( Map.Entry<Object, Plugin> entry : master.entrySet() )
136 {
137 List<Plugin> pre = predecessors.get( entry.getKey() );
138 if ( pre != null )
139 {
140 result.addAll( pre );
141 }
142 result.add( entry.getValue() );
143 }
144 result.addAll( pending );
145
146 target.setPlugins( result );
147 }
148 }
149
150 @Override
151 protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sourceDominant,
152 Map<Object, Object> context )
153 {
154 List<PluginExecution> src = source.getExecutions();
155 if ( !src.isEmpty() )
156 {
157 List<PluginExecution> tgt = target.getExecutions();
158 Map<Object, PluginExecution> merged =
159 new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
160
161 for ( PluginExecution element : tgt )
162 {
163 Object key = getPluginExecutionKey( element );
164 merged.put( key, element );
165 }
166
167 for ( PluginExecution element : src )
168 {
169 Object key = getPluginExecutionKey( element );
170 PluginExecution existing = merged.get( key );
171 if ( existing != null )
172 {
173 mergePluginExecution( existing, element, sourceDominant, context );
174 }
175 else
176 {
177 merged.put( key, element );
178 }
179 }
180
181 target.setExecutions( new ArrayList<>( merged.values() ) );
182 }
183 }
184
185 @Override
186 protected void mergeReporting_Plugins( Reporting target, Reporting source, boolean sourceDominant,
187 Map<Object, Object> context )
188 {
189 List<ReportPlugin> src = source.getPlugins();
190 if ( !src.isEmpty() )
191 {
192 List<ReportPlugin> tgt = target.getPlugins();
193 Map<Object, ReportPlugin> merged =
194 new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
195
196 for ( ReportPlugin element : tgt )
197 {
198 Object key = getReportPluginKey( element );
199 merged.put( key, element );
200 }
201
202 for ( ReportPlugin element : src )
203 {
204 Object key = getReportPluginKey( element );
205 ReportPlugin existing = merged.get( key );
206 if ( existing == null )
207 {
208 merged.put( key, element );
209 }
210 else
211 {
212 mergeReportPlugin( existing, element, sourceDominant, context );
213 }
214 }
215
216 target.setPlugins( new ArrayList<>( merged.values() ) );
217 }
218 }
219
220 @Override
221 protected void mergeReportPlugin_ReportSets( ReportPlugin target, ReportPlugin source, boolean sourceDominant,
222 Map<Object, Object> context )
223 {
224 List<ReportSet> src = source.getReportSets();
225 if ( !src.isEmpty() )
226 {
227 List<ReportSet> tgt = target.getReportSets();
228 Map<Object, ReportSet> merged = new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
229
230 for ( ReportSet element : tgt )
231 {
232 Object key = getReportSetKey( element );
233 merged.put( key, element );
234 }
235
236 for ( ReportSet element : src )
237 {
238 Object key = getReportSetKey( element );
239 ReportSet existing = merged.get( key );
240 if ( existing != null )
241 {
242 mergeReportSet( existing, element, sourceDominant, context );
243 }
244 else
245 {
246 merged.put( key, element );
247 }
248 }
249
250 target.setReportSets( new ArrayList<>( merged.values() ) );
251 }
252 }
253
254 }
255
256 }