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
34
35
36 @Component( role = MojoExecutionConfigurator.class )
37 public class DefaultMojoExecutionConfigurator
38 implements MojoExecutionConfigurator
39 {
40
41 @Override
42 public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig )
43 {
44 String g = mojoExecution.getPlugin().getGroupId();
45
46 String a = mojoExecution.getPlugin().getArtifactId();
47
48 Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
49
50 if ( plugin == null && project.getPluginManagement() != null )
51 {
52 plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
53 }
54
55 if ( plugin != null )
56 {
57 PluginExecution pluginExecution =
58 findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
59
60 Xpp3Dom pomConfiguration = null;
61
62 if ( pluginExecution != null )
63 {
64 pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
65 }
66 else if ( allowPluginLevelConfig )
67 {
68 pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
69 }
70
71 Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
72
73 mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
74
75 mojoExecution.setConfiguration( mojoConfiguration );
76 }
77 }
78
79 private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
80 {
81 for ( Plugin plugin : plugins )
82 {
83 if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
84 {
85 return plugin;
86 }
87 }
88
89 return null;
90 }
91
92 private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
93 {
94 if ( StringUtils.isNotEmpty( executionId ) )
95 {
96 for ( PluginExecution execution : executions )
97 {
98 if ( executionId.equals( execution.getId() ) )
99 {
100 return execution;
101 }
102 }
103 }
104
105 return null;
106 }
107
108 }