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