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