1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle;
20
21 import java.util.Arrays;
22 import java.util.Comparator;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.stream.Collectors;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31 import org.codehaus.plexus.PlexusContainer;
32 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultLifecycles {
46 public static final String[] STANDARD_LIFECYCLES = {"clean", "default", "site", "wrapper"};
47
48 private final Logger logger = LoggerFactory.getLogger(getClass());
49
50
51
52 private final PlexusContainer plexusContainer;
53
54 public DefaultLifecycles() {
55 this.plexusContainer = null;
56 }
57
58 @Inject
59 public DefaultLifecycles(PlexusContainer plexusContainer) {
60 this.plexusContainer = plexusContainer;
61 }
62
63
64
65
66
67
68
69 public Lifecycle get(String phase) {
70 return getPhaseToLifecycleMap().get(phase);
71 }
72
73
74
75
76
77
78
79 public Map<String, Lifecycle> getPhaseToLifecycleMap() {
80
81
82
83 Map<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
84
85 for (Lifecycle lifecycle : getLifeCycles()) {
86 if (logger.isDebugEnabled()) {
87 logger.debug("Lifecycle " + lifecycle);
88 }
89
90 for (String phase : lifecycle.getPhases()) {
91
92 if (!phaseToLifecycleMap.containsKey(phase)) {
93 phaseToLifecycleMap.put(phase, lifecycle);
94 } else {
95 Lifecycle original = phaseToLifecycleMap.get(phase);
96 logger.warn("Duplicated lifecycle phase " + phase + ". Defined in " + original.getId()
97 + " but also in " + lifecycle.getId());
98 }
99 }
100 }
101
102 return phaseToLifecycleMap;
103 }
104
105
106
107
108 public List<Lifecycle> getLifeCycles() {
109 List<String> lifecycleIds = Arrays.asList(STANDARD_LIFECYCLES);
110
111 Comparator<String> comparator = (l, r) -> {
112 int lx = lifecycleIds.indexOf(l);
113 int rx = lifecycleIds.indexOf(r);
114
115 if (lx < 0 || rx < 0) {
116 return rx - lx;
117 } else {
118 return lx - rx;
119 }
120 };
121
122 Map<String, Lifecycle> lifecyclesMap = lookupLifecycles();
123
124
125 return lifecyclesMap.values().stream()
126 .peek(l -> Objects.requireNonNull(l.getId(), "A lifecycle must have an id."))
127 .sorted(Comparator.comparing(Lifecycle::getId, comparator))
128 .collect(Collectors.toList());
129 }
130
131 private Map<String, Lifecycle> lookupLifecycles() {
132
133
134 if (plexusContainer == null) {
135 return new HashMap<>();
136 }
137
138
139 try {
140 return plexusContainer.lookupMap(Lifecycle.class);
141 } catch (ComponentLookupException e) {
142 throw new IllegalStateException("Unable to lookup lifecycles from the plexus container", e);
143 }
144 }
145
146 public String getLifecyclePhaseList() {
147 return getLifeCycles().stream().flatMap(l -> l.getPhases().stream()).collect(Collectors.joining(", "));
148 }
149 }