1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.lifecycle.internal;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.lifecycle.Lifecycle;
29  import org.apache.maven.lifecycle.LifecycleMappingDelegate;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.model.PluginExecution;
32  import org.apache.maven.plugin.BuildPluginManager;
33  import org.apache.maven.plugin.InvalidPluginDescriptorException;
34  import org.apache.maven.plugin.MojoExecution;
35  import org.apache.maven.plugin.MojoNotFoundException;
36  import org.apache.maven.plugin.PluginDescriptorParsingException;
37  import org.apache.maven.plugin.PluginNotFoundException;
38  import org.apache.maven.plugin.PluginResolutionException;
39  import org.apache.maven.plugin.descriptor.MojoDescriptor;
40  import org.apache.maven.project.MavenProject;
41  import org.codehaus.plexus.component.annotations.Component;
42  import org.codehaus.plexus.component.annotations.Requirement;
43  
44  
45  
46  
47  
48  
49  @Component(role = LifecycleMappingDelegate.class, hint = DefaultLifecycleMappingDelegate.HINT)
50  public class DefaultLifecycleMappingDelegate implements LifecycleMappingDelegate {
51      public static final String HINT = "default";
52  
53      @Requirement
54      private BuildPluginManager pluginManager;
55  
56      public Map<String, List<MojoExecution>> calculateLifecycleMappings(
57              MavenSession session, MavenProject project, Lifecycle lifecycle, String lifecyclePhase)
58              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
59                      MojoNotFoundException, InvalidPluginDescriptorException {
60          
61  
62  
63  
64  
65          Map<String, Map<Integer, List<MojoExecution>>> mappings = new LinkedHashMap<>();
66  
67          for (String phase : lifecycle.getPhases()) {
68              Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<>();
69  
70              mappings.put(phase, phaseBindings);
71  
72              if (phase.equals(lifecyclePhase)) {
73                  break;
74              }
75          }
76  
77          
78  
79  
80  
81  
82  
83  
84          for (Plugin plugin : project.getBuild().getPlugins()) {
85              for (PluginExecution execution : plugin.getExecutions()) {
86                  
87                  
88                  if (execution.getPhase() != null) {
89                      Map<Integer, List<MojoExecution>> phaseBindings = mappings.get(execution.getPhase());
90                      if (phaseBindings != null) {
91                          for (String goal : execution.getGoals()) {
92                              MojoExecution mojoExecution = new MojoExecution(plugin, goal, execution.getId());
93                              mojoExecution.setLifecyclePhase(execution.getPhase());
94                              addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
95                          }
96                      }
97                  }
98                  
99                  else {
100                     for (String goal : execution.getGoals()) {
101                         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor(
102                                 plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession());
103 
104                         Map<Integer, List<MojoExecution>> phaseBindings = mappings.get(mojoDescriptor.getPhase());
105                         if (phaseBindings != null) {
106                             MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, execution.getId());
107                             mojoExecution.setLifecyclePhase(mojoDescriptor.getPhase());
108                             addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
109                         }
110                     }
111                 }
112             }
113         }
114 
115         Map<String, List<MojoExecution>> lifecycleMappings = new LinkedHashMap<>();
116 
117         for (Map.Entry<String, Map<Integer, List<MojoExecution>>> entry : mappings.entrySet()) {
118             List<MojoExecution> mojoExecutions = new ArrayList<>();
119 
120             for (List<MojoExecution> executions : entry.getValue().values()) {
121                 mojoExecutions.addAll(executions);
122             }
123 
124             lifecycleMappings.put(entry.getKey(), mojoExecutions);
125         }
126 
127         return lifecycleMappings;
128     }
129 
130     private void addMojoExecution(
131             Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution, int priority) {
132         List<MojoExecution> mojoExecutions = phaseBindings.get(priority);
133 
134         if (mojoExecutions == null) {
135             mojoExecutions = new ArrayList<>();
136             phaseBindings.put(priority, mojoExecutions);
137         }
138 
139         mojoExecutions.add(mojoExecution);
140     }
141 }