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  @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      private Map<String, Lifecycle> customLifecycles;
55  
56      public DefaultLifecycles() {
57          this.plexusContainer = null;
58      }
59  
60      
61  
62  
63      @Deprecated
64      public DefaultLifecycles(Map<String, Lifecycle> lifecycles, org.codehaus.plexus.logging.Logger logger) {
65          this.customLifecycles = lifecycles;
66          this.plexusContainer = null;
67      }
68  
69      @Inject
70      public DefaultLifecycles(PlexusContainer plexusContainer) {
71          this.plexusContainer = plexusContainer;
72      }
73  
74      
75  
76  
77  
78  
79  
80      public Lifecycle get(String phase) {
81          return getPhaseToLifecycleMap().get(phase);
82      }
83  
84      
85  
86  
87  
88  
89  
90      public Map<String, Lifecycle> getPhaseToLifecycleMap() {
91          
92          
93  
94          Map<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
95  
96          for (Lifecycle lifecycle : getLifeCycles()) {
97              logger.debug("Lifecycle {}", lifecycle);
98  
99              for (String phase : lifecycle.getPhases()) {
100                 
101                 if (!phaseToLifecycleMap.containsKey(phase)) {
102                     phaseToLifecycleMap.put(phase, lifecycle);
103                 } else if (logger.isWarnEnabled()) {
104                     Lifecycle original = phaseToLifecycleMap.get(phase);
105                     logger.warn(
106                             "Duplicated lifecycle phase {}. Defined in {} but also in {}",
107                             phase,
108                             original.getId(),
109                             lifecycle.getId());
110                 }
111             }
112         }
113 
114         return phaseToLifecycleMap;
115     }
116 
117     
118 
119 
120     public List<Lifecycle> getLifeCycles() {
121         List<String> lifecycleIds = Arrays.asList(STANDARD_LIFECYCLES);
122 
123         Comparator<String> comparator = (l, r) -> {
124             int lx = lifecycleIds.indexOf(l);
125             int rx = lifecycleIds.indexOf(r);
126 
127             if (lx < 0 || rx < 0) {
128                 return rx - lx;
129             } else {
130                 return lx - rx;
131             }
132         };
133 
134         Map<String, Lifecycle> lifecyclesMap = lookupLifecycles();
135 
136         
137         return lifecyclesMap.values().stream()
138                 .peek(l -> Objects.requireNonNull(l.getId(), "A lifecycle must have an id."))
139                 .sorted(Comparator.comparing(Lifecycle::getId, comparator))
140                 .collect(Collectors.toList());
141     }
142 
143     private Map<String, Lifecycle> lookupLifecycles() {
144         
145         
146         if (plexusContainer == null) {
147             return customLifecycles != null ? customLifecycles : new HashMap<>();
148         }
149 
150         
151         try {
152             return plexusContainer.lookupMap(Lifecycle.class);
153         } catch (ComponentLookupException e) {
154             throw new IllegalStateException("Unable to lookup lifecycles from the plexus container", e);
155         }
156     }
157 
158     public String getLifecyclePhaseList() {
159         return getLifeCycles().stream().flatMap(l -> l.getPhases().stream()).collect(Collectors.joining(", "));
160     }
161 }