1 package com.soebes.maven.plugins.iterator;
2
3 import java.util.ArrayList;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.apache.maven.model.Plugin;
29 import org.apache.maven.plugin.BuildPluginManager;
30 import org.apache.maven.plugin.InvalidPluginDescriptorException;
31 import org.apache.maven.plugin.MojoExecution;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.PluginConfigurationException;
35 import org.apache.maven.plugin.PluginDescriptorParsingException;
36 import org.apache.maven.plugin.PluginManagerException;
37 import org.apache.maven.plugin.PluginResolutionException;
38 import org.apache.maven.plugin.descriptor.MojoDescriptor;
39 import org.apache.maven.plugin.descriptor.PluginDescriptor;
40 import org.apache.maven.plugins.annotations.Component;
41 import org.apache.maven.plugins.annotations.LifecyclePhase;
42 import org.apache.maven.plugins.annotations.Mojo;
43 import org.apache.maven.plugins.annotations.Parameter;
44 import org.apache.maven.plugins.annotations.ResolutionScope;
45 import org.apache.maven.reporting.exec.MavenPluginManagerHelper;
46 import org.codehaus.plexus.configuration.PlexusConfiguration;
47 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
48 import org.codehaus.plexus.util.xml.Xpp3Dom;
49 import org.codehaus.plexus.util.xml.Xpp3DomUtils;
50
51
52
53
54
55
56 @Mojo( name = "iterator", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST, requiresProject = true, threadSafe = true )
57 public class IteratorMojo
58 extends AbstractIteratorMojo
59 {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @Parameter( required = true )
106 private List<PluginExecutor> pluginExecutors;
107
108
109
110
111 @Component
112 protected MavenPluginManagerHelper mavenPluginManagerHelper;
113
114
115
116
117 @Component
118 private BuildPluginManager pluginManager;
119
120 @Parameter( defaultValue = "${plugin}", required = true, readonly = true )
121 private PluginDescriptor pluginDescriptor;
122
123
124
125
126
127 private PlexusConfiguration copyConfiguration( Xpp3Dom src, String iteratorName, String value )
128 {
129
130 XmlPlexusConfiguration dom = new XmlPlexusConfiguration( src.getName() );
131
132 if ( src.getValue() == null )
133 {
134 dom.setValue( src.getValue() );
135 }
136 else
137 {
138 if ( src.getValue().contains( iteratorName ) )
139 {
140 dom.setValue( src.getValue().replaceAll( iteratorName, value ) );
141 }
142 else
143 {
144 dom.setValue( src.getValue() );
145 }
146 }
147
148 for ( String attributeName : src.getAttributeNames() )
149 {
150 dom.setAttribute( attributeName, src.getAttribute( attributeName ) );
151 }
152
153 for ( Xpp3Dom child : src.getChildren() )
154 {
155 dom.addChild( copyConfiguration( child, iteratorName, value ) );
156 }
157
158 return dom;
159 }
160
161
162
163
164
165
166
167
168
169
170 private Plugin getPluginVersionFromPluginManagement( Plugin plugin )
171 {
172
173 if ( !isPluginManagementDefined() )
174 {
175 return plugin;
176 }
177
178 if ( isPluginVersionDefined( plugin ) )
179 {
180 return plugin;
181 }
182
183 Map<String, Plugin> plugins = getMavenProject().getPluginManagement().getPluginsAsMap();
184
185 Plugin result = plugins.get( plugin.getKey() );
186 if ( result == null )
187 {
188 return plugin;
189 }
190 else
191 {
192 return result;
193 }
194 }
195
196 private boolean isPluginVersionDefined( Plugin plugin )
197 {
198 return plugin.getVersion() != null;
199 }
200
201 private boolean isPluginManagementDefined()
202 {
203 return getMavenProject().getPluginManagement() != null;
204 }
205
206 public void execute()
207 throws MojoExecutionException, MojoFailureException
208 {
209
210 if ( isSkip() )
211 {
212 getLog().info( "Skip by user request." );
213 return;
214 }
215
216 if ( isNoneSet() )
217 {
218 getLog().warn("Neither items, itemsWithProperties, content nor folder have been set.");
219 return;
220 }
221
222 if ( isMoreThanOneSet() )
223 {
224 throw new MojoExecutionException( "You can use only one element. "
225 + "Either items, itemsWithProperties, content or folder element but not more than one of them." );
226 }
227
228 List<Exception> exceptions = new ArrayList<>();
229 for ( ItemWithProperties item : getItemsConverted() )
230 {
231 for ( PluginExecutor pluginExecutor : pluginExecutors )
232 {
233 try
234 {
235 handlePluginExecution( item, pluginExecutor );
236 }
237 catch ( MojoExecutionException e )
238 {
239 if ( isFailAtEnd() )
240 {
241 exceptions.add( e );
242 }
243 else
244 {
245 throw e;
246 }
247 }
248 catch ( MojoFailureException e )
249 {
250
251 throw e;
252 }
253 }
254 }
255
256 if ( !exceptions.isEmpty() )
257 {
258 for ( Exception exception : exceptions )
259 {
260 getLog().error( exception );
261 }
262 throw new MojoExecutionException( "Failures during iteration" );
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275 private void handlePluginExecution( ItemWithProperties item, PluginExecutor pluginExecutor )
276 throws MojoExecutionException, MojoFailureException
277 {
278 Plugin executePlugin = getPluginVersionFromPluginManagement( pluginExecutor.getPlugin() );
279 if ( executePlugin.getVersion() == null )
280 {
281 throw new MojoExecutionException( "Unknown plugin version. You have to define the version either directly or via pluginManagement." );
282 }
283
284 Xpp3Dom resultConfiguration = handlePluginConfigurationFromPluginManagement( pluginExecutor, executePlugin );
285
286 PlexusConfiguration plexusConfiguration =
287 copyConfiguration( resultConfiguration, getPlaceHolder(), item.getName() );
288
289 createLogOutput( pluginExecutor, executePlugin, item.getName() );
290
291
292 getMavenProject().getProperties().put( getIteratorName(), item.getName() );
293
294 if ( item.hasProperties() )
295 {
296
297 for ( Entry<Object, Object> entry : item.getProperties().entrySet() )
298 {
299 getMavenProject().getProperties().put( entry.getKey(), entry.getValue() );
300 }
301 }
302
303 try
304 {
305 executeMojo( executePlugin, pluginExecutor.getGoal(), toXpp3Dom( plexusConfiguration ) );
306 }
307 catch ( MojoExecutionException e )
308 {
309 throw e;
310 }
311 catch ( MojoFailureException e )
312 {
313 throw e;
314 }
315 catch ( PluginResolutionException e )
316 {
317 getLog().error( "PluginresourceException:", e );
318 throw new MojoExecutionException( "PluginRescourceException", e );
319 }
320 catch ( PluginDescriptorParsingException e )
321 {
322 getLog().error( "PluginDescriptorParsingException:", e );
323 throw new MojoExecutionException( "PluginDescriptorParsingException", e );
324 }
325 catch ( InvalidPluginDescriptorException e )
326 {
327 getLog().error( "InvalidPluginDescriptorException:", e );
328 throw new MojoExecutionException( "InvalidPluginDescriptorException", e );
329 }
330 catch ( PluginConfigurationException e )
331 {
332 getLog().error( "PluginConfigurationException:", e );
333 throw new MojoExecutionException( "PluginConfigurationException", e );
334 }
335 catch ( PluginManagerException e )
336 {
337 getLog().error( "PluginManagerException:", e );
338 throw new MojoExecutionException( "PluginManagerException", e );
339 }
340 finally
341 {
342
343 getMavenProject().getProperties().remove( getIteratorName() );
344 if ( item.hasProperties() )
345 {
346
347 for ( Object entry : item.getProperties().keySet() )
348 {
349 getMavenProject().getProperties().remove( entry );
350 }
351 }
352
353 }
354 }
355
356 private Xpp3Dom handlePluginConfigurationFromPluginManagement( PluginExecutor pluginExecutor, Plugin executePlugin )
357 {
358 Xpp3Dom resultConfiguration = toXpp3Dom( pluginExecutor.getConfiguration() );
359 getLog().debug( "Configuration: " + resultConfiguration.toString() );
360 if ( executePlugin.getConfiguration() != null )
361 {
362 Xpp3Dom x = (Xpp3Dom) executePlugin.getConfiguration();
363 resultConfiguration = Xpp3DomUtils.mergeXpp3Dom( toXpp3Dom( pluginExecutor.getConfiguration() ), x );
364
365 getLog().debug( "ConfigurationExecutePlugin: " + executePlugin.getConfiguration().toString() );
366
367 }
368 return resultConfiguration;
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 private void executeMojo( Plugin plugin, String goal, Xpp3Dom configuration )
387 throws MojoExecutionException, PluginResolutionException, PluginDescriptorParsingException,
388 InvalidPluginDescriptorException, MojoFailureException, PluginConfigurationException, PluginManagerException
389 {
390
391 if ( configuration == null )
392 {
393 throw new NullPointerException( "configuration may not be null" );
394 }
395
396 PluginDescriptor pluginDescriptor = getPluginDescriptor( plugin );
397
398 MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
399 if ( mojoDescriptor == null )
400 {
401 throw new MojoExecutionException( "Could not find goal '" + goal + "' in plugin " + plugin.getGroupId()
402 + ":" + plugin.getArtifactId() + ":" + plugin.getVersion() );
403 }
404
405 MojoExecution exec = mojoExecution( mojoDescriptor, configuration );
406 pluginManager.executeMojo( getMavenSession(), exec );
407 }
408
409 private PluginDescriptor getPluginDescriptor( Plugin plugin )
410 throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException
411 {
412 return mavenPluginManagerHelper.getPluginDescriptor( plugin, getMavenProject().getRemotePluginRepositories(),
413 getMavenSession() );
414 }
415
416 private MojoExecution mojoExecution( MojoDescriptor mojoDescriptor, Xpp3Dom configuration )
417 {
418 Xpp3Dom resultConfiguration =
419 Xpp3DomUtils.mergeXpp3Dom( configuration, toXpp3Dom( mojoDescriptor.getMojoConfiguration() ) );
420 return new MojoExecution( mojoDescriptor, resultConfiguration );
421 }
422
423
424
425
426
427
428
429 public Xpp3Dom toXpp3Dom( PlexusConfiguration config )
430 {
431 Xpp3Dom result = new Xpp3Dom( config.getName() );
432 result.setValue( config.getValue( null ) );
433 for ( String name : config.getAttributeNames() )
434 {
435 result.setAttribute( name, config.getAttribute( name ) );
436 }
437 for ( PlexusConfiguration child : config.getChildren() )
438 {
439 result.addChild( toXpp3Dom( child ) );
440 }
441 return result;
442 }
443
444
445
446
447
448
449
450 private void createLogOutput( PluginExecutor pluginExecutor, Plugin executePlugin, String item )
451 {
452 StringBuilder sb = new StringBuilder( "------ " );
453 sb.append( "(" );
454 sb.append( item );
455 sb.append( ") " );
456 sb.append( executePlugin.getKey() );
457 sb.append( ":" );
458 sb.append( executePlugin.getVersion() );
459 sb.append( ":" );
460 sb.append( pluginExecutor.getGoal() );
461 getLog().info( sb.toString() );
462 }
463
464 }