View Javadoc

1   package org.apache.maven.model.plugin;
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.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
31  import org.apache.maven.model.Build;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Plugin;
34  import org.apache.maven.model.PluginContainer;
35  import org.apache.maven.model.PluginExecution;
36  import org.apache.maven.model.PluginManagement;
37  import org.apache.maven.model.building.ModelBuildingRequest;
38  import org.apache.maven.model.building.ModelProblemCollector;
39  import org.apache.maven.model.building.ModelProblem.Severity;
40  import org.apache.maven.model.building.ModelProblem.Version;
41  import org.apache.maven.model.building.ModelProblemCollectorRequest;
42  import org.apache.maven.model.merge.MavenModelMerger;
43  import org.codehaus.plexus.component.annotations.Component;
44  import org.codehaus.plexus.component.annotations.Requirement;
45  
46  /**
47   * Handles injection of plugin executions induced by the lifecycle bindings for a packaging.
48   * 
49   * @author Benjamin Bentmann
50   */
51  @Component( role = LifecycleBindingsInjector.class )
52  public class DefaultLifecycleBindingsInjector
53      implements LifecycleBindingsInjector
54  {
55  
56      private LifecycleBindingsMerger merger = new LifecycleBindingsMerger();
57  
58      @Requirement
59      private LifeCyclePluginAnalyzer lifecycle;
60  
61      public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
62      {
63          String packaging = model.getPackaging();
64  
65          Collection<Plugin> defaultPlugins = lifecycle.getPluginsBoundByDefaultToAllLifecycles( packaging );
66  
67          if ( defaultPlugins == null )
68          {
69              problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE)
70                      .setMessage( "Unknown packaging: " + packaging )
71                      .setLocation( model.getLocation( "packaging" )));
72          }
73          else if ( !defaultPlugins.isEmpty() )
74          {
75              Model lifecycleModel = new Model();
76              lifecycleModel.setBuild( new Build() );
77              lifecycleModel.getBuild().getPlugins().addAll( defaultPlugins );
78  
79              merger.merge( model, lifecycleModel );
80          }
81      }
82  
83      private static class LifecycleBindingsMerger
84          extends MavenModelMerger
85      {
86  
87          private static final String PLUGIN_MANAGEMENT = "plugin-management";
88  
89          public void merge( Model target, Model source )
90          {
91              if ( target.getBuild() == null )
92              {
93                  target.setBuild( new Build() );
94              }
95  
96              Map<Object, Object> context =
97                  Collections.<Object, Object> singletonMap( PLUGIN_MANAGEMENT, target.getBuild().getPluginManagement() );
98  
99              mergePluginContainer_Plugins( target.getBuild(), source.getBuild(), false, context );
100         }
101 
102         @Override
103         protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
104                                                      boolean sourceDominant, Map<Object, Object> context )
105         {
106             List<Plugin> src = source.getPlugins();
107             if ( !src.isEmpty() )
108             {
109                 List<Plugin> tgt = target.getPlugins();
110 
111                 Map<Object, Plugin> merged = new LinkedHashMap<Object, Plugin>( ( src.size() + tgt.size() ) * 2 );
112 
113                 for ( Iterator<Plugin> it = tgt.iterator(); it.hasNext(); )
114                 {
115                     Plugin element = it.next();
116                     Object key = getPluginKey( element );
117                     merged.put( key, element );
118                 }
119 
120                 Map<Object, Plugin> unmanaged = new LinkedHashMap<Object, Plugin>();
121 
122                 for ( Iterator<Plugin> it = src.iterator(); it.hasNext(); )
123                 {
124                     Plugin element = it.next();
125                     Object key = getPluginKey( element );
126                     Plugin existing = merged.get( key );
127                     if ( existing != null )
128                     {
129                         mergePlugin( existing, element, sourceDominant, context );
130                     }
131                     else
132                     {
133                         merged.put( key, element );
134                         unmanaged.put( key, element );
135                     }
136                 }
137 
138                 if ( !unmanaged.isEmpty() )
139                 {
140                     PluginManagement pluginMgmt = (PluginManagement) context.get( PLUGIN_MANAGEMENT );
141                     if ( pluginMgmt != null )
142                     {
143                         for ( Iterator<Plugin> it = pluginMgmt.getPlugins().iterator(); it.hasNext(); )
144                         {
145                             Plugin managedPlugin = it.next();
146                             Object key = getPluginKey( managedPlugin );
147                             Plugin unmanagedPlugin = unmanaged.get( key );
148                             if ( unmanagedPlugin != null )
149                             {
150                                 Plugin plugin = managedPlugin.clone();
151                                 mergePlugin( plugin, unmanagedPlugin, sourceDominant, Collections.emptyMap() );
152                                 merged.put( key, plugin );
153                             }
154                         }
155                     }
156                 }
157 
158                 List<Plugin> result = new ArrayList<Plugin>( merged.values() );
159 
160                 target.setPlugins( result );
161             }
162         }
163 
164         @Override
165         protected void mergePluginExecution( PluginExecution target, PluginExecution source, boolean sourceDominant,
166                                              Map<Object, Object> context )
167         {
168             super.mergePluginExecution( target, source, sourceDominant, context );
169 
170             target.setPriority( Math.min( target.getPriority(), source.getPriority() ) );
171         }
172 
173     }
174 
175 }