View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle;
20  
21  import java.util.Arrays;
22  import java.util.Comparator;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.stream.Collectors;
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * @since 3.0
38   * @author Jason van Zyl
39   * @author Kristian Rosenvold
40   */
41  // TODO The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the
42  // wiring and reference and external source for the lifecycle configuration.
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      // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml")
51  
52      private final PlexusContainer plexusContainer;
53  
54      public DefaultLifecycles() {
55          this.plexusContainer = null;
56      }
57  
58      @Inject
59      public DefaultLifecycles(PlexusContainer plexusContainer) {
60          this.plexusContainer = plexusContainer;
61      }
62  
63      /**
64       * Get lifecycle based on phase
65       *
66       * @param phase
67       * @return
68       */
69      public Lifecycle get(String phase) {
70          return getPhaseToLifecycleMap().get(phase);
71      }
72  
73      /**
74       * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the
75       * phase they want to execute and we can easily determine what lifecycle we need to run.
76       *
77       * @return A map of lifecycles, indexed on id
78       */
79      public Map<String, Lifecycle> getPhaseToLifecycleMap() {
80          // If people are going to make their own lifecycles then we need to tell people how to namespace them correctly
81          // so that they don't interfere with internally defined lifecycles.
82  
83          Map<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
84  
85          for (Lifecycle lifecycle : getLifeCycles()) {
86              if (logger.isDebugEnabled()) {
87                  logger.debug("Lifecycle " + lifecycle);
88              }
89  
90              for (String phase : lifecycle.getPhases()) {
91                  // The first definition wins.
92                  if (!phaseToLifecycleMap.containsKey(phase)) {
93                      phaseToLifecycleMap.put(phase, lifecycle);
94                  } else {
95                      Lifecycle original = phaseToLifecycleMap.get(phase);
96                      logger.warn("Duplicated lifecycle phase " + phase + ". Defined in " + original.getId()
97                              + " but also in " + lifecycle.getId());
98                  }
99              }
100         }
101 
102         return phaseToLifecycleMap;
103     }
104 
105     /**
106      * Returns an ordered list of lifecycles
107      */
108     public List<Lifecycle> getLifeCycles() {
109         List<String> lifecycleIds = Arrays.asList(STANDARD_LIFECYCLES);
110 
111         Comparator<String> comparator = (l, r) -> {
112             int lx = lifecycleIds.indexOf(l);
113             int rx = lifecycleIds.indexOf(r);
114 
115             if (lx < 0 || rx < 0) {
116                 return rx - lx;
117             } else {
118                 return lx - rx;
119             }
120         };
121 
122         Map<String, Lifecycle> lifecyclesMap = lookupLifecycles();
123 
124         // ensure canonical order of standard lifecycles
125         return lifecyclesMap.values().stream()
126                 .peek(l -> Objects.requireNonNull(l.getId(), "A lifecycle must have an id."))
127                 .sorted(Comparator.comparing(Lifecycle::getId, comparator))
128                 .collect(Collectors.toList());
129     }
130 
131     private Map<String, Lifecycle> lookupLifecycles() {
132         // TODO: Remove the following code when maven-compat is gone
133         // This code is here to ensure maven-compat's EmptyLifecycleExecutor keeps on working.
134         if (plexusContainer == null) {
135             return new HashMap<>();
136         }
137 
138         // Lifecycles cannot be cached as extensions might add custom lifecycles later in the execution.
139         try {
140             return plexusContainer.lookupMap(Lifecycle.class);
141         } catch (ComponentLookupException e) {
142             throw new IllegalStateException("Unable to lookup lifecycles from the plexus container", e);
143         }
144     }
145 
146     public String getLifecyclePhaseList() {
147         return getLifeCycles().stream().flatMap(l -> l.getPhases().stream()).collect(Collectors.joining(", "));
148     }
149 }