1   package org.apache.maven.lifecycle.internal;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
61  
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  
81  
82  
83  
84  
85  
86          for ( Plugin plugin : project.getBuild().getPlugins() )
87          {
88              for ( PluginExecution execution : plugin.getExecutions() )
89              {
90                  
91                  
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                 
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 }