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