View Javadoc
1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }