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