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.ArrayList;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.lifecycle.Lifecycle;
30  import org.apache.maven.lifecycle.LifecycleMappingDelegate;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.model.PluginExecution;
33  import org.apache.maven.plugin.BuildPluginManager;
34  import org.apache.maven.plugin.InvalidPluginDescriptorException;
35  import org.apache.maven.plugin.MojoExecution;
36  import org.apache.maven.plugin.MojoNotFoundException;
37  import org.apache.maven.plugin.PluginDescriptorParsingException;
38  import org.apache.maven.plugin.PluginNotFoundException;
39  import org.apache.maven.plugin.PluginResolutionException;
40  import org.apache.maven.plugin.descriptor.MojoDescriptor;
41  import org.apache.maven.project.MavenProject;
42  import org.codehaus.plexus.component.annotations.Component;
43  import org.codehaus.plexus.component.annotations.Requirement;
44  
45  /**
46   * Lifecycle mapping delegate component interface. Calculates project build execution plan given {@link Lifecycle} and
47   * lifecycle phase. Standard lifecycles use plugin execution {@code <phase>} or mojo default lifecycle phase to
48   * calculate the execution plan, but custom lifecycles can use alternative mapping strategies.
49   */
50  @Component( role = LifecycleMappingDelegate.class, hint = DefaultLifecycleMappingDelegate.HINT )
51  public class DefaultLifecycleMappingDelegate
52      implements LifecycleMappingDelegate
53  {
54      public static final String HINT = "default";
55  
56      @Requirement
57      private BuildPluginManager pluginManager;
58  
59      public Map<String, List<MojoExecution>> calculateLifecycleMappings( MavenSession session, MavenProject project,
60                                                                          Lifecycle lifecycle, String lifecyclePhase )
61          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
62          MojoNotFoundException, InvalidPluginDescriptorException
63      {
64          /*
65           * Initialize mapping from lifecycle phase to bound mojos. The key set of this map denotes the phases the caller
66           * is interested in, i.e. all phases up to and including the specified phase.
67           */
68  
69          Map<String, Map<Integer, List<MojoExecution>>> mappings =
70              new LinkedHashMap<>();
71  
72          for ( String phase : lifecycle.getPhases() )
73          {
74              Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<>();
75  
76              mappings.put( phase, phaseBindings );
77  
78              if ( phase.equals( lifecyclePhase ) )
79              {
80                  break;
81              }
82          }
83  
84          /*
85           * Grab plugin executions that are bound to the selected lifecycle phases from project. The effective model of
86           * the project already contains the plugin executions induced by the project's packaging type. Remember, all
87           * phases of interest and only those are in the lifecycle mapping, if a phase has no value in the map, we are
88           * not interested in any of the executions bound to it.
89           */
90  
91          for ( Plugin plugin : project.getBuild().getPlugins() )
92          {
93              for ( PluginExecution execution : plugin.getExecutions() )
94              {
95                  // if the phase is specified then I don't have to go fetch the plugin yet and pull it down
96                  // to examine the phase it is associated to.
97                  if ( execution.getPhase() != null )
98                  {
99                      Map<Integer, List<MojoExecution>> phaseBindings = mappings.get( execution.getPhase() );
100                     if ( phaseBindings != null )
101                     {
102                         for ( String goal : execution.getGoals() )
103                         {
104                             MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() );
105                             mojoExecution.setLifecyclePhase( execution.getPhase() );
106                             addMojoExecution( phaseBindings, mojoExecution, execution.getPriority() );
107                         }
108                     }
109                 }
110                 // if not then i need to grab the mojo descriptor and look at the phase that is specified
111                 else
112                 {
113                     for ( String goal : execution.getGoals() )
114                     {
115                         MojoDescriptor mojoDescriptor =
116                             pluginManager.getMojoDescriptor( plugin, goal, project.getRemotePluginRepositories(),
117                                                              session.getRepositorySession() );
118 
119                         Map<Integer, List<MojoExecution>> phaseBindings = mappings.get( mojoDescriptor.getPhase() );
120                         if ( phaseBindings != null )
121                         {
122                             MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, execution.getId() );
123                             mojoExecution.setLifecyclePhase( mojoDescriptor.getPhase() );
124                             addMojoExecution( phaseBindings, mojoExecution, execution.getPriority() );
125                         }
126                     }
127                 }
128             }
129         }
130 
131         Map<String, List<MojoExecution>> lifecycleMappings = new LinkedHashMap<>();
132 
133         for ( Map.Entry<String, Map<Integer, List<MojoExecution>>> entry : mappings.entrySet() )
134         {
135             List<MojoExecution> mojoExecutions = new ArrayList<>();
136 
137             for ( List<MojoExecution> executions : entry.getValue().values() )
138             {
139                 mojoExecutions.addAll( executions );
140             }
141 
142             lifecycleMappings.put( entry.getKey(), mojoExecutions );
143         }
144 
145         return lifecycleMappings;
146 
147     }
148 
149     private void addMojoExecution( Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution,
150                                    int priority )
151     {
152         List<MojoExecution> mojoExecutions = phaseBindings.get( priority );
153 
154         if ( mojoExecutions == null )
155         {
156             mojoExecutions = new ArrayList<>();
157             phaseBindings.put( priority, mojoExecutions );
158         }
159 
160         mojoExecutions.add( mojoExecution );
161     }
162 
163 }