1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.mapping;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import static java.util.function.Function.identity;
27 import static java.util.stream.Collectors.toMap;
28
29
30
31
32 public class DefaultLifecycleMapping implements LifecycleMapping {
33
34 private List<Lifecycle> lifecycles;
35
36 private Map<String, Lifecycle> lifecycleMap;
37
38
39 private Map<String, LifecyclePhase> phases;
40
41
42
43
44
45
46
47 @Deprecated
48 public DefaultLifecycleMapping() {}
49
50
51
52
53 public DefaultLifecycleMapping(final List<Lifecycle> lifecycles) {
54 this.lifecycleMap =
55 Collections.unmodifiableMap(lifecycles.stream().collect(toMap(Lifecycle::getId, identity())));
56 }
57
58
59
60
61 private void initLifecycleMap() {
62 if (lifecycleMap == null) {
63 lifecycleMap = new HashMap<>();
64
65 if (lifecycles != null) {
66 for (Lifecycle lifecycle : lifecycles) {
67 lifecycleMap.put(lifecycle.getId(), lifecycle);
68 }
69 } else {
70
71
72
73
74
75 String[] lifecycleIds = {"default", "clean", "site"};
76
77 for (String lifecycleId : lifecycleIds) {
78 Map<String, LifecyclePhase> phases = getLifecyclePhases(lifecycleId);
79 if (phases != null) {
80 Lifecycle lifecycle = new Lifecycle();
81
82 lifecycle.setId(lifecycleId);
83 lifecycle.setLifecyclePhases(phases);
84
85 lifecycleMap.put(lifecycleId, lifecycle);
86 }
87 }
88 }
89 }
90 }
91
92 @Override
93 public Map<String, Lifecycle> getLifecycles() {
94 initLifecycleMap();
95
96 return lifecycleMap;
97 }
98
99 @Deprecated
100 @Override
101 public List<String> getOptionalMojos(String lifecycle) {
102 return null;
103 }
104
105 private Map<String, LifecyclePhase> getLifecyclePhases(String lifecycle) {
106 initLifecycleMap();
107
108 Lifecycle lifecycleMapping = lifecycleMap.get(lifecycle);
109
110 if (lifecycleMapping != null) {
111 return lifecycleMapping.getLifecyclePhases();
112 } else if ("default".equals(lifecycle)) {
113 return phases;
114 } else {
115 return null;
116 }
117 }
118
119 @Deprecated
120 public Map<String, String> getPhases(String lifecycle) {
121 return LifecyclePhase.toLegacyMap(getLifecyclePhases(lifecycle));
122 }
123 }