View Javadoc
1   package org.apache.maven.lifecycle;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @since 3.0
41   * @author Jason van Zyl
42   * @author Kristian Rosenvold
43   */
44  // TODO The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the
45  // wiring and reference and external source for the lifecycle configuration.
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      // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml")
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       * Get lifecycle based on phase
71       *
72       * @param phase
73       * @return
74       */
75      public Lifecycle get( String phase )
76      {
77          return getPhaseToLifecycleMap().get( phase );
78      }
79  
80      /**
81       * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the
82       * phase they want to execute and we can easily determine what lifecycle we need to run.
83       *
84       * @return A map of lifecycles, indexed on id
85       */
86      public Map<String, Lifecycle> getPhaseToLifecycleMap()
87      {
88          // If people are going to make their own lifecycles then we need to tell people how to namespace them correctly
89          // so that they don't interfere with internally defined lifecycles.
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                 // The first definition wins.
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      * Returns an ordered list of lifecycles
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         // ensure canonical order of standard lifecycles
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         // TODO: Remove the following code when maven-compat is gone
153         // This code is here to ensure maven-compat's EmptyLifecycleExecutor keeps on working.
154         if ( plexusContainer == null )
155         {
156             return new HashMap<>();
157         }
158 
159         // Lifecycles cannot be cached as extensions might add custom lifecycles later in the execution.
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 }