1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collection;
23
24 import org.apache.maven.lifecycle.MojoExecutionConfigurator;
25 import org.apache.maven.model.Plugin;
26 import org.apache.maven.model.PluginExecution;
27 import org.apache.maven.plugin.MojoExecution;
28 import org.apache.maven.project.MavenProject;
29 import org.codehaus.plexus.component.annotations.Component;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.codehaus.plexus.util.xml.Xpp3Dom;
32
33 @Component( role = MojoExecutionConfigurator.class )
34 public class DefaultMojoExecutionConfigurator
35 implements MojoExecutionConfigurator
36 {
37
38 @Override
39 public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig )
40 {
41 String g = mojoExecution.getGroupId();
42
43 String a = mojoExecution.getArtifactId();
44
45 Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
46
47 if ( plugin == null && project.getPluginManagement() != null )
48 {
49 plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
50 }
51
52 if ( plugin != null )
53 {
54 PluginExecution pluginExecution =
55 findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
56
57 Xpp3Dom pomConfiguration = null;
58
59 if ( pluginExecution != null )
60 {
61 pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
62 }
63 else if ( allowPluginLevelConfig )
64 {
65 pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
66 }
67
68 Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
69
70 mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
71
72 mojoExecution.setConfiguration( mojoConfiguration );
73 }
74 }
75
76 private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
77 {
78 for ( Plugin plugin : plugins )
79 {
80 if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
81 {
82 return plugin;
83 }
84 }
85
86 return null;
87 }
88
89 private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
90 {
91 if ( StringUtils.isNotEmpty( executionId ) )
92 {
93 for ( PluginExecution execution : executions )
94 {
95 if ( executionId.equals( execution.getId() ) )
96 {
97 return execution;
98 }
99 }
100 }
101
102 return null;
103 }
104
105 }