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  /**
34   * @since 3.3.1, MNG-5753
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 }