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