001package org.apache.maven.lifecycle.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collection;
023
024import org.apache.maven.lifecycle.MojoExecutionConfigurator;
025import org.apache.maven.model.Plugin;
026import org.apache.maven.model.PluginExecution;
027import org.apache.maven.plugin.MojoExecution;
028import org.apache.maven.project.MavenProject;
029import org.codehaus.plexus.component.annotations.Component;
030import org.codehaus.plexus.util.StringUtils;
031import org.codehaus.plexus.util.xml.Xpp3Dom;
032
033/**
034 * @since 3.3.1, MNG-5753
035 */
036@Component( role = MojoExecutionConfigurator.class )
037public class DefaultMojoExecutionConfigurator
038    implements MojoExecutionConfigurator
039{
040
041    @Override
042    public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig )
043    {
044        String g = mojoExecution.getGroupId();
045
046        String a = mojoExecution.getArtifactId();
047
048        Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
049
050        if ( plugin == null && project.getPluginManagement() != null )
051        {
052            plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
053        }
054
055        if ( plugin != null )
056        {
057            PluginExecution pluginExecution =
058                findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
059
060            Xpp3Dom pomConfiguration = null;
061
062            if ( pluginExecution != null )
063            {
064                pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
065            }
066            else if ( allowPluginLevelConfig )
067            {
068                pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
069            }
070
071            Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
072
073            mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
074
075            mojoExecution.setConfiguration( mojoConfiguration );
076        }
077    }
078
079    private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
080    {
081        for ( Plugin plugin : plugins )
082        {
083            if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
084            {
085                return plugin;
086            }
087        }
088
089        return null;
090    }
091
092    private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
093    {
094        if ( StringUtils.isNotEmpty( executionId ) )
095        {
096            for ( PluginExecution execution : executions )
097            {
098                if ( executionId.equals( execution.getId() ) )
099                {
100                    return execution;
101                }
102            }
103        }
104
105        return null;
106    }
107
108}