001package org.apache.maven.model.plugin;
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.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
030import org.apache.maven.model.Build;
031import org.apache.maven.model.Model;
032import org.apache.maven.model.Plugin;
033import org.apache.maven.model.PluginContainer;
034import org.apache.maven.model.PluginExecution;
035import org.apache.maven.model.PluginManagement;
036import org.apache.maven.model.building.ModelBuildingRequest;
037import org.apache.maven.model.building.ModelProblemCollector;
038import org.apache.maven.model.building.ModelProblem.Severity;
039import org.apache.maven.model.building.ModelProblem.Version;
040import org.apache.maven.model.building.ModelProblemCollectorRequest;
041import org.apache.maven.model.merge.MavenModelMerger;
042import org.codehaus.plexus.component.annotations.Component;
043import org.codehaus.plexus.component.annotations.Requirement;
044
045/**
046 * Handles injection of plugin executions induced by the lifecycle bindings for a packaging.
047 *
048 * @author Benjamin Bentmann
049 */
050@Component( role = LifecycleBindingsInjector.class )
051public class DefaultLifecycleBindingsInjector
052    implements LifecycleBindingsInjector
053{
054
055    private LifecycleBindingsMerger merger = new LifecycleBindingsMerger();
056
057    @Requirement
058    private LifeCyclePluginAnalyzer lifecycle;
059
060    public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
061    {
062        String packaging = model.getPackaging();
063
064        Collection<Plugin> defaultPlugins = lifecycle.getPluginsBoundByDefaultToAllLifecycles( packaging );
065
066        if ( defaultPlugins == null )
067        {
068            problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
069                    .setMessage( "Unknown packaging: " + packaging )
070                    .setLocation( model.getLocation( "packaging" ) ) );
071        }
072        else if ( !defaultPlugins.isEmpty() )
073        {
074            Model lifecycleModel = new Model();
075            lifecycleModel.setBuild( new Build() );
076            lifecycleModel.getBuild().getPlugins().addAll( defaultPlugins );
077
078            merger.merge( model, lifecycleModel );
079        }
080    }
081
082    protected static class LifecycleBindingsMerger
083        extends MavenModelMerger
084    {
085
086        private static final String PLUGIN_MANAGEMENT = "plugin-management";
087
088        public void merge( Model target, Model source )
089        {
090            if ( target.getBuild() == null )
091            {
092                target.setBuild( new Build() );
093            }
094
095            Map<Object, Object> context =
096                Collections.<Object, Object>singletonMap( PLUGIN_MANAGEMENT, target.getBuild().getPluginManagement() );
097
098            mergePluginContainer_Plugins( target.getBuild(), source.getBuild(), false, context );
099        }
100
101        @Override
102        protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
103                                                     boolean sourceDominant, Map<Object, Object> context )
104        {
105            List<Plugin> src = source.getPlugins();
106            if ( !src.isEmpty() )
107            {
108                List<Plugin> tgt = target.getPlugins();
109
110                Map<Object, Plugin> merged = new LinkedHashMap<>( ( src.size() + tgt.size() ) * 2 );
111
112                for ( Plugin element : tgt )
113                {
114                    Object key = getPluginKey( element );
115                    merged.put( key, element );
116                }
117
118                Map<Object, Plugin> unmanaged = new LinkedHashMap<>();
119
120                for ( Plugin element : src )
121                {
122                    Object key = getPluginKey( element );
123                    Plugin existing = merged.get( key );
124                    if ( existing != null )
125                    {
126                        mergePlugin( existing, element, sourceDominant, context );
127                    }
128                    else
129                    {
130                        merged.put( key, element );
131                        unmanaged.put( key, element );
132                    }
133                }
134
135                if ( !unmanaged.isEmpty() )
136                {
137                    PluginManagement pluginMgmt = (PluginManagement) context.get( PLUGIN_MANAGEMENT );
138                    if ( pluginMgmt != null )
139                    {
140                        for ( Plugin managedPlugin : pluginMgmt.getPlugins() )
141                        {
142                            Object key = getPluginKey( managedPlugin );
143                            Plugin unmanagedPlugin = unmanaged.get( key );
144                            if ( unmanagedPlugin != null )
145                            {
146                                Plugin plugin = managedPlugin.clone();
147                                mergePlugin( plugin, unmanagedPlugin, sourceDominant, Collections.emptyMap() );
148                                merged.put( key, plugin );
149                            }
150                        }
151                    }
152                }
153
154                List<Plugin> result = new ArrayList<>( merged.values() );
155
156                target.setPlugins( result );
157            }
158        }
159
160        @Override
161        protected void mergePluginExecution( PluginExecution target, PluginExecution source, boolean sourceDominant,
162                                             Map<Object, Object> context )
163        {
164            super.mergePluginExecution( target, source, sourceDominant, context );
165
166            target.setPriority( Math.min( target.getPriority(), source.getPriority() ) );
167        }
168
169    }
170
171}