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@Component( role = MojoExecutionConfigurator.class )
034public class DefaultMojoExecutionConfigurator
035    implements MojoExecutionConfigurator
036{
037
038    @Override
039    public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig )
040    {
041        String g = mojoExecution.getGroupId();
042
043        String a = mojoExecution.getArtifactId();
044
045        Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
046
047        if ( plugin == null && project.getPluginManagement() != null )
048        {
049            plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
050        }
051
052        if ( plugin != null )
053        {
054            PluginExecution pluginExecution =
055                findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
056
057            Xpp3Dom pomConfiguration = null;
058
059            if ( pluginExecution != null )
060            {
061                pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
062            }
063            else if ( allowPluginLevelConfig )
064            {
065                pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
066            }
067
068            Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
069
070            mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
071
072            mojoExecution.setConfiguration( mojoConfiguration );
073        }
074    }
075
076    private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
077    {
078        for ( Plugin plugin : plugins )
079        {
080            if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
081            {
082                return plugin;
083            }
084        }
085
086        return null;
087    }
088
089    private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
090    {
091        if ( StringUtils.isNotEmpty( executionId ) )
092        {
093            for ( PluginExecution execution : executions )
094            {
095                if ( executionId.equals( execution.getId() ) )
096                {
097                    return execution;
098                }
099            }
100        }
101
102        return null;
103    }
104
105}